Java exceptions

package exceptions;

public class CatchingExceptionExample {

    public static void main(String[] args) {
        int i = 0;
        int result = 0;
        try {
            result = 10 / i;
        } catch (ArithmeticException ae) {
            System.out.println("In catch ArithmeticException: " + ae.getMessage() + ", result=" + result);
        } finally {
            System.out.println("In finally, result=" + result);
        }
        System.out.println("Out of try-catch-finally, result=" + result);
    }

}

// In catch ArithmeticException: / by zero, result=0
// In finally, result=0
// Out of try-catch-finally, result=0
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s