Class 17 Exception

Embed Size (px)

Citation preview

  • 8/4/2019 Class 17 Exception

    1/15

    Object Oriented Programmingwww.ru.ac.bd/cse/~az

    Object OrientedProgramming

    With Java

  • 8/4/2019 Class 17 Exception

    2/15

    Object Oriented Programingwww.ru.ac.bd/cse/~az

    2

    Exception Handling

    Exception ?

    Instance that does not follow a rule

    Error

  • 8/4/2019 Class 17 Exception

    3/15

    Object Oriented Programingwww.ru.ac.bd/cse/~az

    3

    Exception Handling

    Suppose an error occurs while a Java program is running.

    The error might be caused by

    a file containing wrong information,

    a flaky network connection, or

    use of an invalid array index or

    an attempt to use an object reference that hasn't yet been assignedto an object.

  • 8/4/2019 Class 17 Exception

    4/15

    Object Oriented Programingwww.ru.ac.bd/cse/~az

    4

    Exception Handling

    If an operation cannot be completed because of an error, theprogram ought to either:

    Return to a safe state and enable the user to execute other

    commands;

    or

    Allow the user to save all his or her work and terminate the program

    gracefully.

    This may not be easy to do,

  • 8/4/2019 Class 17 Exception

    5/15

    Object Oriented Programingwww.ru.ac.bd/cse/~az

    5

    Exception Handling

    The mission of exception handling is to transfer control

    fromwhere the error occurred

    toan error-handler that can deal with the situation.

    To handle exceptional situations

    you must take into account the errors and problems thatmay occur.

    What sorts of problems do you need toconsider?

  • 8/4/2019 Class 17 Exception

    6/15

    Object Oriented Programingwww.ru.ac.bd/cse/~az

    6

    Exception Handling

    What sorts of problems do you need toconsider?

    User input errors

    Int

    char

  • 8/4/2019 Class 17 Exception

    7/15

    Object Oriented Programingwww.ru.ac.bd/cse/~az

    7

    Exception Handling

  • 8/4/2019 Class 17 Exception

    8/15

    Object Oriented Programingwww.ru.ac.bd/cse/~az

    8

  • 8/4/2019 Class 17 Exception

    9/15

    Object Oriented Programingwww.ru.ac.bd/cse/~az

    9

    Exception Handling

    To catch an exception,

    you set up a try/catch block.

    The simplest form of the try block is as follows:

  • 8/4/2019 Class 17 Exception

    10/15

    Object Oriented Programingwww.ru.ac.bd/cse/~az

    10

    Exception Handling

    If any of the code inside the try block throws an exception of theclass specified in the catch clause, then,

    1. The program skips the remainder of the code in the try block;

    2. The program executes the handler code inside the catch clause.

  • 8/4/2019 Class 17 Exception

    11/15

    Object Oriented Programingwww.ru.ac.bd/cse/~az

    11

    Exception Handling

    If none of the code inside the try block throws an exception, then

    the program skips the catch clause.

    If any of the code in a method throws an exception of a typeother than the one named in the catch clause,

    this method exits immediately.

  • 8/4/2019 Class 17 Exception

    12/15

    Object Oriented Programingwww.ru.ac.bd/cse/~az

    12

  • 8/4/2019 Class 17 Exception

    13/15

    Object Oriented Programingwww.ru.ac.bd/cse/~az

    13

    Exception Handling

  • 8/4/2019 Class 17 Exception

    14/15

    Object Oriented Programingwww.ru.ac.bd/cse/~az

    14

    finally block

    Programs that obtain certain types of resources must return them~ to avoid ~ resource leaks

  • 8/4/2019 Class 17 Exception

    15/15

    Object Oriented Programingwww.ru.ac.bd/cse/~az

    15

    Exception Handling

    Java guarantees that

    a finally block will execute whether or not an exception is thrown in thecorresponding try block or any of its corresponding catch blocks.

    Java also guarantees that

    a finally block (if one is present) will execute if a try block exits by using a return, break or continue statement.

    The finally block will notexecute if the application exits early froma try block by calling method System.exit.

    Immediately terminates an application.