(Java)Hirarki Class Exception

Exception handling is a very important yet often neglected aspect of writing robust software. When an error occurs in a Java program it usually results in an exception being thrown. How you throw, catch and handle these exception matters. There are several different ways to do so. Not all are equally efficient and fail safe.

This trail digs deeper into exception handling in Java. The trail covers various do’s and dont’s of Java exception handling. It also covers a few techniques for efficient and less error prone exception handling. Hopefully you can benefit from some of these texts.

The version of Java used in this tutorial is Java 6 and Java 7, though most of the techniques here work already from Java 5 and forward. Some even from Java 4.

Below follows a quick introduction to what you can learn in this Java exception handling trail.

Structure

try {  
//statements  
//resource-acquisition statements  
} // end try  
catch ( AKindOfException exception1 ) {  
//exception-handling statements  
} // end catch  
.  
.  
.  
catch ( AnotherKindOfException exception2 ) {  
//exception-handling statements  
} // end catch  
finally {  
//statements  
//resource-release statements  
} 
<pre> 

Example Write Here

public class Main {  
  
    public static void main(String[] args) {  
        try {  
            throwException();  
        } catch (Exception exception) {  
            System.err.println("Exception handled in main");  
        }  
        doesntThrowException();  
    }  
  
    public static void throwException() throws Exception {  
        try {  
            System.out.println("Method throwException Called");  
            throw new Exception();  
        } catch (Exception exception) {  
            System.err.println("Exception handled in method throwException");  
            throw exception;  
        } finally {  
            System.err.println("Finally executed in throwException");  
        }  
    }  
  
    public static void doesntThrowException() {  
        try {  
            System.out.println("Method doesntthrowException Called");  
        } catch (Exception exception) {  
            System.err.println(exception);  
        } finally {  
            System.err.println("Finally executed in doesntthrowException");  
        }  
        System.out.println("End of method doesNotThrowException");  
    }  
}  
<pre> 

Reference :
tutorials.jenkov.
Ebook Java Desktop Ifnu Bima

Advertisements

Published by

jarcode

hello I am jarcode

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