JaiswalTraining
Get the online training

Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com





Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven







What is an Exception?
  • An event during program execution that prevents the program from continuing normally 
  • An error condition that changes the normal flow of control in a program 
  • A signal that some unexpected condition has occurred in the program 


Handling Exceptions in Java


  • Exception mechanism is built around the throw-and-catch paradigm
    • “to throw” means an exception has occurred
    • “to catch” means to deal with an exception

  • If an exception is not caught, it is propagated to the call stack until a handler is found
  • Propagating an exception is called “ducking” the exception, or “passing the buck”
Types of Exceptions

  • All exceptions in Java are objects of Throwable class
  • Unchecked Exceptions
    • are exceptions derived from Error and RuntimeException classes
    • are usually irrecoverable and not handled explicitly
    • are not checked by the compiler
  • Checked Exceptions
    • are exceptions derived from Exception class excluding the RuntimeException class
    • must be handled explicitly
    • are checked by the compiler
  • Both checked and unchecked exceptions can be thrown and caught
  • New exceptions are created by extending the Exception class or its subclasses

Exception handling

  • Without Exception handling codes will give the error message on the console.
  • It also terminates the program and  all the codes present after exception will not execute.
  • It creates the problem to the end user.
  • He may think that software is corrupt.
  • Example below gives the bulky error message  which user may
    not understand.
  • If this is handled then a short message will displayed and the user will
    realise his error
public class div {

  public static void main(String[] args) {

      int x = 10 0;
      int ar[] 23};

      System.out.println("Division is " + x);
      System.out.println(ar[11]);
  }
}

Output

java div
Exception in thread "main" java.lang.ArithmeticException: / by zero
        at div.main(div.java:5)

Download code


Example This example demonstrates the above program with try and catch
So it will display the small user defined  message.

public class myerr {

  public static void main(String[] args) {
    try {
      int x = 10 0;
      int ar[] 23};

      System.out.println("Division is " + x);
      System.out.println(ar[11]);
    catch (Exception e) {
      System.err.println("Divided by zero error");
    }
  }
}

Output

javac myerr.java
java myerr
Divided by zero error





Download code