Exception handling in Java
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
  • nice website
    Are you sure you want to
    Your message goes here
    Be the first to like this
No Downloads

Views

Total Views
7,785
On Slideshare
7,784
From Embeds
1
Number of Embeds
1

Actions

Shares
Downloads
145
Comments
1
Likes
0

Embeds 1

http://www.slideshare.net 1

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Exceptions Mr.PrasadSawantDepartment Of Computer ScienceProf .Ramkurshana More Arts, Commerce and Science College Akurdi. Mobile :9665755707 Email:sawanprasad@gmail.comBlog:http://prasadmsawant.blogspot.com/
  • 2. Agenda
    Define exception.
    Discuss what is meant by exception handing.
    Describe the try,catch and finally blocks.
    Examine multiple catch blocks.
    Explore nested try catch blocks.
    Explain the use of throw and throws keywords.
    Create user defined exception.
  • 3. What is exception?
    When an error is encountered during the execution of a program an exception is said to have occurred.
  • 4. Handling Exception

    IF B IS ZERO GO TO EROR
    C=A/B
    PRINT C
    GO TO EXIT
    ERROR:
    DISPLAY “CODE CAUSING ERROR DUE TO DIVISION BY ZERO”
    EXIT:
    END
    BLOCK THAT HANDLES
    ERROR
  • 5. HANDLING EXCEPTION IN JAVA
    OBJECT
    Error
    THROWABLE
    Exception
    SQLException
    AWT ERROR
    ClassNotFoundException
    Thread Death..
    ………
    ………
  • 6. System-Defined Exception
    Raised implicitly by system because of illegal execution of program
    When cannot continue program execution any more
    Created by Java System automatically
    Exception extended from Error class and RuntimeException class
    • [DivByZero.java]
  • IndexOutOfBoundsException:
    When beyond the bound of index in the object which use index, such as array, string, and vector
    ArrayStoreException :
    When assign object of incorrect type to element of array
    NegativeArraySizeException :
    When using a negative size of array
    NullPointerException :
    When refer to object as a null pointer
    SecurityException :
    When violate security. Caused by security manager
    IllegalMonitorStateException :
    When the thread which is not owner of monitor involves wait or notify method
  • 7. Programmer-Defined Exception
    Exceptions raised by programmer
    Check by compiler whether the exception handler for exception occurred exists or not
    If there is no handler, it is error
    Sub class of Exception class
  • 8. The exception handling model
    try
    {
    // code which is expected to throw an exception
    }
    Catch (exception e1)
    {
    /*
    If exception is thrown in the try block ,is of type e1,then perform necessary action here
    */
    }
  • 9. Multiple catch block
    Class catch22
    {
    public static void main(String args[])
    {
    try
    {
    String num=args[0];
    intnumValue=Interger.parseInt(num);
    System.out.println(“The squre is ”+numValue*numvalue”);
    }
    catch(ArrayIndexOutOfBoundException e1)
    {
    System.out.println(“No argument is given!”);
    }
    catch(NumberFormatException e2)
    {
    System.out.println(“Not a number “);
    }
    }
    }
    C:>javac catch22
    No argument is given!
    C:>javac catch22 a
    Not a number
    C:>javac catch22 3
    The squre is 9
  • 10. Nested try-catch

    try
    {
    int num=args.length;
    try
    {
    intnumValue=Integer.parseInt(args[0]);
    System.out.println(“The squre is “+numValue*numValue);
    }
    catch(NumberFormatExcption e1)
    {
    System.out.println(“Not a Number!”);
    }
    }
    catch (ArrayIndexOutOfBoundException e2)
    {
    System.out.println(“No arguments given!”);
    }
    ….
    C:>javac catch
    No argument is given!
    C:>javac catch22 a
    Not a number
    C:>javac catch22 3
    The squre is 9
  • 11. Finally block
    The ‘finally ’block is guaranteed to run whether or not an exception occurs. The finally clauses of a try catch block will always execute ,even if there are return statement in the try catch part .
    Try block
    No exception
    Exception
    finally
    Catch block
    finally
  • 12. Using throws and throw
    Exception are thrown with the help of the ‘throw’ keyword .The ‘throw’ keyword is used to indicate that an exception has occurred .The operand of throw is an object of any class that is derived from the class ’Throwable’.
    The throws keyword is used to list the exception that method can throw.