Topic 4 Exception Handling Part II

Embed Size (px)

Citation preview

  • 7/30/2019 Topic 4 Exception Handling Part II

    1/43

    Programming Language (JAVA)

    Unit 6.10 Exception Handling

    Presentation2

  • 7/30/2019 Topic 4 Exception Handling Part II

    2/43

    Objectives

    At the end of this presentation, you will be able to : Demonstrate the use of try, catch, finally blocks

    List the built in exceptions

    Create user defined exception

  • 7/30/2019 Topic 4 Exception Handling Part II

    3/43

    try block

    The exceptions in a program are caught or

    trapped using a try block.

    A block of code which may generate anexception to terminate the program should be

    placed inside the tryblock.

  • 7/30/2019 Topic 4 Exception Handling Part II

    4/43

    try block

    Syntax

    try

    {

    statements ;// code which generates exception

    }

  • 7/30/2019 Topic 4 Exception Handling Part II

    5/43

    try block - Example

    try

    {

    int A,B,C;A=Integer.parseInt(args[0]);

    B=Integer.parseInt(args[1]);

    C=A/B;

    System.out.println(The value of C:- + C);

    }

  • 7/30/2019 Topic 4 Exception Handling Part II

    6/43

    catch block

    A try block should have at least one catchblock.

    The try block may or may not generate anexception.

    The catch block is responsible for catchingthe exception thrown from the try block.

    Hence, a catch block is placed immediatelyafter a try block.

  • 7/30/2019 Topic 4 Exception Handling Part II

    7/43

    catch block

    Syntax

    catch(Exceptiontype object)

    {

    statements;// code which handles exception

    }

  • 7/30/2019 Topic 4 Exception Handling Part II

    8/43

    catch block

    Example

    catch(ArithmeticException exp)

    {

    System.out.println(I have caught the

    exception);

    }

  • 7/30/2019 Topic 4 Exception Handling Part II

    9/43

    Exception Handling Mechanism

  • 7/30/2019 Topic 4 Exception Handling Part II

    10/43

    Hands-On!

    Program excep1.javauses tryand catchblocks to handle the generated exception -

    ArithmeticException

  • 7/30/2019 Topic 4 Exception Handling Part II

    11/43

    Hands-On!

    Program ArrayExcepDemo.javageneratesArrayIndexOutOfBoundsException.

  • 7/30/2019 Topic 4 Exception Handling Part II

    12/43

    Activity 6.10.3

    Fill in the blanks:

    Step 1: Open the excep3.java file.

    1. class excep32. {

    3. public static void main(_______ args[])

    4. {

    5. int a=10,b=5,c=5,x=0;

    6. _____

  • 7/30/2019 Topic 4 Exception Handling Part II

    13/43

  • 7/30/2019 Topic 4 Exception Handling Part II

    14/43

    Activity 6.10.3 (Contd..)

    Fill in the blanks:

    12. System.out.println(The exception is caught);

    13. }

    14. System.out.println(The value of x is :- +x);

    15. }

    16. }

    Step 2: Read the program and fill in the blanks withappropriate code.

    Step 3: Save the program.

    Step 4: Compile and execute it.

  • 7/30/2019 Topic 4 Exception Handling Part II

    15/43

    Activity 6.10.4

    1. Give the output for program excep4.java.

    Step 1: Open excep4.java file.

    Step 2: Read the program, predict and write the

    output.

  • 7/30/2019 Topic 4 Exception Handling Part II

    16/43

    Lab Exercise

    1.Type the code of program error2.java.

    class error2

    {public static void main(String args[])

    {

    int a=Integer.parseInt(args[0]);int b=Integer.parseInt(args[1]);

    int c=Integer.parseInt(args[2]);

  • 7/30/2019 Topic 4 Exception Handling Part II

    17/43

    Lab Exercise (Contd..)

    int d=Integer.parseInt(args[3]);

    double Result= a+b*c/d;

    System.out.println(The value of theexpression is + Result);

    }

    }

  • 7/30/2019 Topic 4 Exception Handling Part II

    18/43

    Lab Exercise (Contd..)

    1. Compile and execute the program error2.java

    2. Enter positive values for a, b, c and d.

    3. See the result.

    4. Execute the program again and enter the valueof d as 0. An arithmetic exception occurs.

    5. Add try and catch blocks to handle thisexception.

    6. Compile, execute the program and see theresult.

  • 7/30/2019 Topic 4 Exception Handling Part II

    19/43

    Multiple Catch

    Some times a code can generate more than

    one type of exception.

    If more than one type of exception arises,more than one catch statement should be

    used to handle those exception types.

  • 7/30/2019 Topic 4 Exception Handling Part II

    20/43

    Hands-On!

    Program MultiCatchDemo.javaillustrates theuse of multi catch statement.

  • 7/30/2019 Topic 4 Exception Handling Part II

    21/43

    Hands-On!

    Program ExcepClassDemo.javaillustrates theuse ofExceptionclass to catch any type of

    exception generated by the program.

  • 7/30/2019 Topic 4 Exception Handling Part II

    22/43

    Lab Exercise

    2. Write a program to find the sum of 10

    numbers in an array. Use try, catch block to

    handle ArrayIndexOutOfBoundsException.

  • 7/30/2019 Topic 4 Exception Handling Part II

    23/43

    throw statement

    The throw statement allows you to throw

    exceptions that are not thrown by Java run-

    time system. When throwis used inside the program, it

    implies that the program is going to throw an

    exception.

  • 7/30/2019 Topic 4 Exception Handling Part II

    24/43

    throw statement

    Syntax

    throw new exceptiontype ();

  • 7/30/2019 Topic 4 Exception Handling Part II

    25/43

    Hands-On!

    Program throwdemo.javaillustrates the useof throw statement.

  • 7/30/2019 Topic 4 Exception Handling Part II

    26/43

    Activity 6.10.5

    Fill in the blanks to complete the program thatthrows exception explicitly:

    Step 1: Open the excep5.java student data file.

    Step 2: Read the program and fill in the blanks withappropriate code.

    1. _______ excep5

    2. {

    3. public static void main(String args[])

    4. {

    5. int a=5,b=10,c=0;

  • 7/30/2019 Topic 4 Exception Handling Part II

    27/43

    Activity 6.10.5 (Contd..)6. c = b/a;7. try8. {9. ________ new Exception(What will happen);10. }

    11. catch(____________ ae)12. {13. System.out.println(The exception caught);14. }15. System.out.println(The value of c is :- +c);16. }

    17. }

    Step 3: Save the program.

    Step 4: Compile and execute it.

  • 7/30/2019 Topic 4 Exception Handling Part II

    28/43

    finally

    The finallyblock contains statements fordoing the final process such as de-allocationof memory etc.

    It may be added immediately after the tryblock or after the last catchblock.

    A try block should have at least one catch

    block or finally block immediately following it.

  • 7/30/2019 Topic 4 Exception Handling Part II

    29/43

    finally

    Syntax

    finally

    {

    statements ;// code to be executed

    }

  • 7/30/2019 Topic 4 Exception Handling Part II

    30/43

    Hands-On!

    Program finallydemo.javademonstrates the

    use of finally block.

  • 7/30/2019 Topic 4 Exception Handling Part II

    31/43

    Activity 6.10.6

    Detect the errors in the following program:

    Step 1: Open excep6.java student data file.

    1. class excep62. {

    3. public static void main(String args[])

    4. {5. int a=0,b=5,c=0;

    6. try

    7. {

  • 7/30/2019 Topic 4 Exception Handling Part II

    32/43

    Activity 6.10.6 (Contd..)

    8. c=b/a;

    9. }

    10. catch(ArithemticException e)

    11. {

    12. System.out.println("Iam from catch block");

    13. }

  • 7/30/2019 Topic 4 Exception Handling Part II

    33/43

    Activity 6.10.6 (Contd..)

    14. final

    15. {

    16. System.out.println("I am from finally block");

    17. }

    18. }

    19. }

    Step 2:Read the program and detect the errors in program.

  • 7/30/2019 Topic 4 Exception Handling Part II

    34/43

    Activity 6.10.7

    Detect the errors in the following program:

    Step 1: Open excep7.java student data file.

    class excep7{

    public static void main(String args[])

    {int a[]={1,2};

    try

    {

  • 7/30/2019 Topic 4 Exception Handling Part II

    35/43

    Activity 6.10.7 (Contd..)

    if (a[1]>a[2])

    System.out.println(a[1]);

    Else

    System.out.println(a[2]);

    }

    catch(ArrayIndexOutOfBoundsException e)

    {

  • 7/30/2019 Topic 4 Exception Handling Part II

    36/43

    Activity 6.10.7 (Contd..)

    System.out.println("I am from catch block");

    }

    finally

    {

    System.out.println("I am from finally block");

    }

    }}

    Step 2:Read the program and correct the mistakes.Step 3: Save, compile and execute the program.

  • 7/30/2019 Topic 4 Exception Handling Part II

    37/43

    Lab Exercise

    3. Write a program to get 5 values of array

    elements from the command line and print

    those values in the main() method. Add trycatch blocks to handle

    ArrayIndexOutOfBoundsException

    4. Write a program to generate two exceptions

    and catch using multiple catch blocks. Add a

    finally block that prints The program is over.

  • 7/30/2019 Topic 4 Exception Handling Part II

    38/43

    User-Defined Exceptions

    The built-in exceptions handle most of thecommon errors.

    Certain application specific exceptions maynot be covered in the built-in exceptions.

    Create a user-defined exception to handle thesituation.

    Use exception handling mechanism to avoidthe abnormal program termination.

  • 7/30/2019 Topic 4 Exception Handling Part II

    39/43

    User-Defined Exceptions

  • 7/30/2019 Topic 4 Exception Handling Part II

    40/43

    Hands-On!

    Program myexception.javaillustrates thecreation of User-Defined Exception.

  • 7/30/2019 Topic 4 Exception Handling Part II

    41/43

    Lab Exercise

    5. Write a program to generate an exception

    by your own (user defined exception) and

    handle the exception.

  • 7/30/2019 Topic 4 Exception Handling Part II

    42/43

    Summary

    In this presentation, you learnt the following:

    finally block is used to handle any exception

    generated by try block, even if the catchblock is not able handle the exception.

    The finally block contains statements for

    doing the final process such as de-allocationof memory etc.

    The class Exception is the subclass of theclass Throwable.

  • 7/30/2019 Topic 4 Exception Handling Part II

    43/43

    Assignment

    1. Define exception.

    2. Write the syntax and example for a try

    block.

    3. Is it essential to capture all types of

    exceptions?

    4. What is the use of finally block?