samedi 23 janvier 2021

When to throw specific exception and when to output error to console using system.out

I was designing an oop model where I got confused between throwing exception and outputting it through system.out using if else I was doing like

public void fun(int n) {
if(n<0){
    System.out.println("n less than 0");
}
}

I could have also done like

public void fun(int n) throws InvalidNumberException{
if(n<0){
    InvalidNumberException("n less than 0");
}
}

Which is the good way to actually do that. Should I throw exception and catch it or just use if else to output.

Aucun commentaire:

Enregistrer un commentaire