user2613413 user2613413 - 3 years ago 120
Java Question

Difference between "throw new Exception" and "new Exception"?

I am interested to know best practice to use "Throw new exception" and "new exception". In case of using "new exception", I have seen that code moves to next statement instead of throwing exception.

But I am told that we should "new Exception" to throw Runtime Exception.

Can anyone throw some light on this ?


Answer Source

new Exception() means create an instance (same as creating new Integer(...)) but no exception will happen until you throw it...

Consider following snippet:

public static void main(String[] args) throws Exception {

    private static void foo2(final int number) throws Exception {
    Exception ex;
    if (number < 0) {
        ex = new Exception("No negative number please!");
        // throw ex; //nothing happens until you throw it


    private static void foo(final int number) throws Exception {
    if (number < 0) {
        throw new Exception("No negative number please!");


the method foo() will THROW an exception if the parameter is negative but the method foo2() will create an instance of exception if the parameter is negative

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download