2 Examples to Show How Java Exception Handling Works

There are 2 examples below. One shows all caller methods also need to handle exceptions thrown by the callee method. The other one shows the super class can be used to catch or handle subclass exceptions.

Caller method must handle exceptions thrown by the callee method

Here is a program which handles exceptions. Just test that, if an exception is thrown in one method, not only that method, but also all methods which call that method have to declare or throw that exception.

public class exceptionTest {
    private static Exception exception;
 
    public static void main(String[] args) throws Exception {
            callDoOne(); 
    }
 
    public static void doOne() throws Exception {
        throw exception;
    }
 
    public static void callDoOne() throws Exception {
        doOne();
    }
}

The super class can be used to catch or handle subclass exceptions

The following is also OK, because the super class can be used to catch or handle subclass exceptions:

class myException extends Exception{
 
}
 
public class exceptionTest {
    private static Exception exception;
    private static myException myexception;
 
    public static void main(String[] args) throws Exception {
            callDoOne(); 
    }
 
    public static void doOne() throws myException {
        throw myexception;
    }
 
    public static void callDoOne() throws Exception {
        doOne();
        throw exception;
    }
}

This is the reason that only one parent class in the catch clause is syntactically safe.

Category >> Exceptions  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>