0
import java.io.*;

class West1 extends Exception {
    private String msg;
    public West1() {
    }

    public West1(String msg) {
        super(msg);
        this.msg=msg;
    }

    public West1(Throwable cause) {
        super(cause);
    }

    public West1(String msg,Throwable cause) {
        super(msg,cause);
        this.msg=msg;
    }


    public String toString() {
        return msg;
    }


    public String getMessage() {
        return msg;
    }
}

public class West {
    public static void main(String[] args) {
        try {
            throw new West1("Custom Exception.....");
        }catch(West1 ce) {
            System.out.println(ce.getMessage());
            //throw new NumberFormatException();
            throw new FileNotFoundException();
        }catch(FileNotFoundException fne) {
            fne.printStackTrace();  
        }/*catch(NumberFormatException nfe) {
            nfe.printStackTrace();
        }*/
    }
}

In the above code, NumberFormatException is thrown from catch block it compile and run successfully but when FileNotFoundException is thrown from catch block it will not compile. Following Errors are thrown:

West.java:40: error: exception FileNotFoundException is never thrown in body of
corresponding try statement
                }catch(FileNotFoundException fne){
West.java:39: error: unreported exception FileNotFoundException; must be caught
or declared to be thrown
                        throw new FileNotFoundException();

So my question is what is reason behind this behaviour?

Captain Man
  • 6,997
  • 6
  • 48
  • 74
  • 4
    Take a look at checked vs unchecked explanation: http://stackoverflow.com/questions/6115896/java-checked-vs-unchecked-exception-explanation – Alex Baker May 02 '15 at 15:18
  • What the message says. You must add FileNotFoundException to the `throws` clause of the method, if you're letting it "escape". – Hot Licks May 02 '15 at 21:36

2 Answers2

0

NumberFormatException is a RuntimeException, meaning that it's not required to be declared in all methods it may be thrown. This means that, unlike FileNotFoundException, the compiler can not know if it can get thrown in a block or not.

Maurice Perry
  • 32,610
  • 9
  • 70
  • 97
0

The title implies you are trying to catch multiple exceptions at once, and your code implies you understand that you can have multiple catch blocks for a single try block to catch different types of exceptions. Everything is good so far, but you seem to misunderstand exactly where the try-catch catches errors.

Here is you code. I removed the comments to make it more concise.

public class West {
    public static void main(String[] args) {
        try {
            throw new West1("Custom Exception.....");
        } catch(West1 ce) {
            System.out.println(ce.getMessage());
            throw new FileNotFoundException(); // <-- Must be caught
        } catch(FileNotFoundException fne) { // <-- Never thrown
            fne.printStackTrace();  
        }
    }
}

The first compiler error is because the catch block that is for catching FileNotFoundException's try block never throws FileNotFoundException. The second compiler error is because FileNotFoundException is a checked exception. Because it is a checked exception your code must either

  • handle it (by catching it with try-catch)
  • let everyone know it could throw it (public static void main(String[] args) throws FileNotFoundException { ...).

From the context of your code, you seem to be going with the first option, handling it with try-catch, but you have the catch in the wrong place.

catch blocks don't catch exceptions, try blocks do. catch blocks specify what to do with the actual caught exception.

public class West {
    public static void main(String[] args) {
        try {
            throw new West1("Custom Exception.....");
        } catch(West1 ce) {
            System.out.println(ce.getMessage());
            try {
                throw new FileNotFoundException();
            } catch(FileNotFoundException fne) {
                fne.printStackTrace();
            }
        }
    }
}

Try that instead. Notice how you can have a try instead of a catch. This wouldn't matter if FileNotFoundException wasn't a checked exception.

Captain Man
  • 6,997
  • 6
  • 48
  • 74
  • Please provide explanation as to why you downvote when you do. I feel my answer answers OP's question well, but will still edit to be more clear. – Captain Man May 04 '15 at 18:13