Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Throwing exception in main method

Tags:

java

exception

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();
        } 
like image 302
Andrey Chasovski Avatar asked Jul 13 '13 10:07

Andrey Chasovski


People also ask

How do you terminate a method that throws an exception?

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.

What happens when exception is thrown by main method in Java?

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.

What are the rules for exception handling for main method?

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.

What happens if a method throws a filenotfoundexception?

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.


2 Answers

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);
  }
}
like image 144
Enigmadan Avatar answered Oct 14 '22 01:10

Enigmadan


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.

like image 42
Juned Ahsan Avatar answered Oct 14 '22 01:10

Juned Ahsan