Q:

Difference between Exceptions and if then in Java

I have 2 examples:
void terminate(String x) {
     System.exit(0);
}

void x (long y) {
if(y == null) {
    terminate();
}
//code
}
.....
x(null); //Don't want that

This example seems to work. I want to make sure that long y is not null. I might do this instead:
void x (long y) throws NullPointerException {
//code
}
...
try {
x(null);
} catch (NullPointerException e) {
 e.printstacktrace();
}

What would the difference in performance be? In such example, it might not be visible, but in a complex program, would it matter? Should I get into practice of exceptions instead of if then?

A:

Use exceptions for exceptional circumstances, when things happen that youdid not expect and where nothing sensible can be done to recover.
Otherwise, use if-then and maybe return a status code.
In terms of performance, exceptions create a huge overhead compared to returns. Calling a method and catching an exception if it didn't work is verbose and annoying.
void myOperation() {
   step1();
   step2();
   step3();
}

In this example, exceptions are preferable for when any of the steps fails, the entire operation fails anyways, so I prefer not having to deal with any of this.
void myOperation(String number) {
    int n;
    try { 
        n = Integer.parse(number);
    } catch (NumberFormatException e) {
        n = DEFAULT;
    }
    doThings(n);
}

In this case, having to catch the exception is annoying because I kind of expect the string to be invalid and have a reasonable alternative. The exception makes the code slow and verbose.

