jeudi 13 septembre 2018

What is the difference between a try-catch statement and an if-else statement?

Specifically,what is the difference between using try-catch statement

try {
    do.method1(Object obj1) 
    // method might throw an NullPointerException caused by null parameter obj1
} 
catch (NullPointerException e) {
    System.out.println(" No such object");
}

and using if-statement

public boolean method1(Object obj1) {
    boolean res = false;
    if (obj1 == null) {
        return false;
    }
    ...
}

when dealing with an exception?

Aucun commentaire:

Enregistrer un commentaire