Andrey Chasovski
Andrey Chasovski

Reputation: 145

Throwing exception in main method

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();
        } 

Upvotes: 12

Views: 43282

Answers (7)

verdaux
verdaux

Reputation: 1

Throwing an exception is a way of indicating to the calling/invoking method to be prepared to manage an unusual scenario.

As main method is the root-most in the hierarchy the propagation will end here.

The need for handling arises when we intend a clean exit. In your case, you're doing just that with the try/catch block.

https://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html

Upvotes: 0

Juned Ahsan
Juned Ahsan

Reputation: 68715

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.

Upvotes: 3

Sajol
Sajol

Reputation: 51

You are handling your exception in your code using try-catch. This is the reason.

Upvotes: 0

Enigmadan
Enigmadan

Reputation: 3408

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);
  }
}

Upvotes: 10

M Sach
M Sach

Reputation: 34424

Basically checked exceptions needs to be handled. Compiler forces to do that. You can do that either by try catch or by throws clause . One of them is sufficient.

You would resort to first approach when you don't want to handle that exception by calling code.

Upvotes: 0

dinukadev
dinukadev

Reputation: 2297

Sometimes the calling party will not want an exception thrown because they will not want to handle such exceptions. In such instances we do enclose the code that could throw exceptions with a try catch finally block.

But in instances where you want to specifically catch a certain exception and try to do something about it, or if you want to throw out a customer exception to the calling party, then you should first catch it.

It is recommended never to swallow exceptions and also not to catch and re-throw the same exception.

Upvotes: 1

anttix
anttix

Reputation: 7779

Simple answer:

You don't need to declare a method to throw if all the checked exceptions are handled by the code.

See also:

http://www.javapractices.com/topic/TopicAction.do?Id=129

Upvotes: 2

Related Questions