I am trying to figure out why I have to throw
exception in the main method while I have try
/catch
blocks that can handle those exceptions anyway?
Even if I delete throws IllegalArgumentException,InputMismatchException
part, the program will still compile and work perfectly.
public static void main(String[] args) throws IllegalArgumentException,InputMismatchException{
boolean flag = true;
Scanner in = new Scanner(System.in);
do{
try{
System.out.println("Please enter the number:");
int n = in.nextInt();
int sum = range(n);
System.out.println("sum = " + sum);
flag = false;
}
catch(IllegalArgumentException e){
System.out.println(e.getMessage());
}
catch(InputMismatchException e){
System.out.println("The number has to be as integer...");
in.nextLine();
}
The main method should simply terminate if any exception occurs. The throws clause only states that the method throws a checked FileNotFoundException and the calling method should catch or rethrow it. If a non-checked exception is thrown (and not catch) in the main method, it will also terminate.
So when exception is thrown by main method then JVM prints the exception to standard console and terminates. It is valid to add throws with main method. The rules for exception for main method is same as it is for other methods.
The rules for exception for main method is same as it is for other methods. When throws is added to method declaration then it is the responsibility of the calling method to handle exception. Here JVM calls the main method, so thrown exception is handled by JVM.
The throws clause only states that the method throws a checked FileNotFoundException and the calling method should catch or rethrow it. If a non-checked exception is thrown (and not catch) in the main method, it will also terminate.
You only throw an exception if you want it to be handled by a "higher" function.
(Note: The exception doesn't just disappear when it is thrown. It still has to be handled.)
public void functionA() throws Exception{
throw new Exception("This exception is going to be handled elsewhere");
}
You use a try/catch
block when you want to handle the exception immediately.
public void functionB(){
try{
throw new Exception("This exception is handled here.");
}catch(Exception e){
System.err.println("Exception caught: "+e);
}
}
If you are already using a try/catch
block to catch an exception, then you have no need to throw that exception any higher.
public void functionC() throws Exception{
try{
throw new Exception("This exception doesn't know where to go.");
}catch(Exception e){
System.err.println("Exception caught: "+e);
}
}
Any method has two choices to deal with the exceptions that can occur in that method:
First choice is to handle the exception within the method using a catch and don't tell anyone about it. This approach is useful in handling errors, which will have no effect on the method calling this.
Second choice is to catch the exception in the method, may or may not do something to handle the exception. Additionally tell the calling method that something has gone wrong, so you do the needful. This approach is useful and should be used for exceptions which are causing a problem that need to be propagated above to the calling hierarchy.
I don't think it is really a good idea to throw exceptions form the main method. Because even if you don't throw it, JVM will get the exception and will exit. The best you can do is to try to catch those excepitons and do some corrective action within the main. If the exception is catastrophic no matter whether you throw it or not, the program will exit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With