Home | Login | Blog | Forum | Download | RSS Feed | Ajax | Hello-World | J2EE | BPM | EJB | FileNet | Hibernate | ORM | IBM | ILOG | Interview | FAQs |

Java | Apache | JVM | Others | BRMS | Oracle | Pega | SOA | Sun-Certification | Web-Service | websphere | XML |
 

JAVA DEVELOPER SITE - COMPLETE JAVA TUTORIALS

Java Exception Handling Mechanism - Tutorials and Source Code


Share
 
 

Useful Java Links


       


 A Java exception is an object that describes an exceptional condition which has occurred in a piece of code. Many things can lead to exceptions , including hardware failures, resource exhaustion and bugs in the program. When an exceptional event occurs in the Java program , an exception is thrown.  The code that is responsible for doing something about the exception is called “exception handler”.

            The Exception handling works by transferring the execution of a program to an appropriate exception handler when an exception occurs. Java exception handling is managed via five keywords: try, catch, throw, throws, and finally. We have tell the JVM what code to execute when certain exceptional condition occurs in the program. To do this we use try and catch keywords. Within a try block we will write a code in which exception may occur. This block of code is called guarded region. One or more catch clauses  match a specific exception to a block of code that handles it.

 

            Here is the basic form of exception handling block.

 

                        try{

                                //block of code

                        }catch ( ExceptionType1 ex1){

                                    // exception handler for ExcpetionType1.

                        } catch(ExceptionType2 ex2){

                                    //Exception handler for ExcpetionType2.

                                    throw(e);

                        }finally{

                        }

Java Exception Type Hierarchy:.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


·         Throwable class is used represent all exceptional conditions. This is superclass of all Exception types.

·         Two immediate  subclass of Throwable class is Error and Exception.

·         Exception is used for exceptional conditions that user programs should catch.

·         Exception class is used to create our own custom exception. Just we have to create a subclass of Exception and we create our exceptional conditions in this class that user program to catch.

·         Example for Exception Class

            public  class Propagate {

 

              public static void main(String[] args) {

                     String str="pramila";

                     Propagate p=new Propagate();

                     try{

                                  String revStr=p.reverse(str);

                                  System.out.println(revStr);

                           }catch(StringIndexOutOfBoundsException stEx){

                                  System.out.println("Exception"+stEx.getMessage());

                           }catch(IndexOutOfBoundsException ie){

                                  System.out.println("Exception"+ie.getMessage());

                           }finally{

                           System.out.println("Exception occured in finally block");

                              try{

                                         System.out.println(" In finally block try block                                          Value"+5/0);

                                  }catch(ArithmeticException ie){

                                         System.out.println("Exception                                                            occured"+ie.getMessage());

                                  }

             

                              }                

             

        }

       public String reverse(String str){

              String reveString = "";

              for(int i=str.length();i>0;i--){

                     reveString += str.charAt(i);

              }

              return reveString;

       }

}

 

·         Throw statement is used to explicitly throw an exception. First we have to get a handle on an instance of Throwable, via a parameter in to a catch clause or by creating one using  new operator. General form of throw statement is throw ThrowableInstance;

·         Throws keyword is used to identify the list of possible exceptions that a method might throw. General form of throws statement is type method-name(arg-list) throws exception-list {}

·         Sometimes it is necessary to be assured that given piece of code will run no matter which exceptions are caused and caught. The finally keyword can be used to identify such a block of code.

·         It is illegal to use a try clause without either a catch or finally  clause.

·         There are two types of exceptions

   Checked Exception: - Checked exception must be explicitly caught or propagated. It                                                      extends the class java.lang.Exception

 

Unchecked Exception: Is not explicitly caught or propagated. It extends the the class                                                                    java.lang.RuntimeException

 

   

 

·         Error defines the conditions that should not be expected to be caught under normal circumstances. Error are typically created in response to catastrophic failures. For Example StackoverflowError. The most common way this to occur is to create a recursive method. Look at the following code

                  void go(){

                        go();

                        }

            Here if we make a mistake of invoking the go() method, our program will fall into a black hole; go invoking go() invoking go, until, no matter how much memory we have, we will get a StackOverflowError. Here only JVM knows when this event occurs, and the JVM will be the source of this error.




Download Latest Java/J2EE eBooks, SCJP Dumps, SCWCD Dumps, Pega Tutorial, IBM WebSphere BPM tutorials and many more


If you need any urgent assistance on Fundamentals-of-Java-Exception-Handling, kindly email your requirement to us at : info@javagenious.com or Contact-an-Expert. Our experts will try their best to solve your problem.

You can also subscribe to our newsletters on Fundamentals-of-Java-Exception-Handling, to receive updates on Fundamentals-of-Java-Exception-Handling,via email.Enter you email below:

Enter your email address:

Delivered by FeedBurner



Keyword Tags: Fundamentals-of-Java-Exception-Handling tutorial,Fundamentals-of-Java-Exception-Handling in java,Concepts of Fundamentals-of-Java-Exception-Handling,Java,J2EE,Interview Questions,Fundamentals-of-Java-Exception-Handling Examples


Comments:


Post Your Comment:

*

Yes, I would like to recieve email notifications on my reply.

Popular Posts:

 
Popular Downloads

SCJP 1.6 Dumps
JSP Interview Questions
Struts Interview Questions
Hibernate Interview Questions
SCWCD Dumps
SCBCD Dumps

Java/J2EE Tutorial
JMS Tutorials
seam richfaces tutorial
richfaces live demo
jquery example demo
JQuery Tutorial and Source Code
JSF Tutorial
XSL Tutorial
J2EE JMS
CORBA Applications
Java CERTIFICATION
SCJP 1.5
SCJP 1.6
SCWCD 1.5
SCBCD
Mis
Ajax autosuggest autocomplete from database
Struts forms and validation with webflow
Java Apache POI Examples
JDBC Tutorial
Oracle 9i & 10g
MYSQL Tutorial
php Tutorial
JQuery Examples
Upload Java File
JAXB Examples
PL-SQL Tutorial
Dojo AJAX
EJB Examples
Java IDE
Eclipse
NetBeans
IBM Rational Weblogic Workshop
Downloads
SCJP 1.6 Dumps
SCJP 1.5 DUMP
SCJP Material
SCWCD
Java Security
SCBCD Dumps
External Links
SUN Java
JOracle
IBM
PHP Certification
Technical Interview Questions
Amazon Interview Questions
Google Interview Questions
Microsoft Interview Questions
IBM Interview Questions
Yahoo Interview Questions
All Tutorials
php mysql tutorial
SCJP 1.5/1.6
SCWCD 1.5/1.6
Polymorphism
Thread Tutorial
Generics & Exceptions
Spring Struts Hibernate
Java Collections
MAP
SET
LIST
VECTOR
Advanced Java
Stateless Session Bean
Introduction to EJB
Stateful Session Bean
Java Design Patterns
HOME | FORUM | ABOUT | BLOGS | CONTACT-US