If I have a program that invokes a method which must terminate if a particular exception is thrown, should I design it such that the exception is passed up back to main
so it can safely return, or should I handle the exception in the function and invoke System.exit(-1)
?
Here is an example of what I mean by handling it in the function:
public class MyClass{
public static void main (String[] args){
myFunction(args);
}
public static void myFunction(String[] args){
try{
if(args.length == 0)
throw Exception("Invalid Input");
}catch(Exception e){
System.out.println(e);
System.exit(-1);
}
}
}
Here is an example of what I mean by passing it up:
public class MyClass{
public static void main (String[] args){
try{
myFunction(args);
}catch(Exception e){
System.out.println(e);
}
}
public static void myFunction(String[] args) throws Exception{
if(args.length == 0)
throw Exception("Invalid Input");
}
}
Which way should be used in this situation?