Exception handling

Here is a little program about handling exceptions. Just test that, if a exception is thrown in one method, not only that method, but also all methods whichs calls the method,  have to declare or throws exceptions.

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

You may also like:

Leave a comment