Exception Handling - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Exception Handling

Description:

Unchecked Exceptions. Don't need to be handled by the programmer (an option) Errors ... Used to enclose a block of code where an exception can take place. catch ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 12
Provided by: jpe897
Category:

less

Transcript and Presenter's Notes

Title: Exception Handling


1
Exception Handling
  • Chapter 10

2
Exceptions Handling
  • Allows a programmer to catch abnormal condition
    that arise in a code sequence
  • Keywords
  • try
  • catch
  • throw
  • throws
  • finally

3
Exception Types
  • Run-time Exceptions
  • Exceptions that can be handled by your programs
  • Checked Exceptions
  • Always required to catch
  • Unchecked Exceptions
  • Dont need to be handled by the programmer (an
    option)
  • Errors
  • Can not be handled by the programs
  • Out of the programmers control

4
try and catch
  • try
  • Used to enclose a block of code where an
    exception can take place
  • catch
  • Will jump to this area of code if a particular
    type of exception needs to be handled

5
An Example
  • class Test
  • public static void main (String args )
  • int a 0
  • try
  • a 84 / (math.Random( ) 7)
  • System.out.println(When will this line be
    skipped?)
  • catch (ArithmeticException e)
  • System.out.println(Division by zero.)
  • a 0

6
Multiple catch clauses
  • class Test
  • .
  • .
  • try
  • .
  • catch (ArithmeticException e)
  • catch (IOException e)
  • catch (Exception e)

7
Nested try statements
  • class Test
  • .
  • .
  • try
  • .
  • try //NESTED if exception is not handled
    here
  • catch // it will go up to the next
    exception try to find
  • // the next place to handle the exception
  • catch (ArithmeticException e)
  • catch (Exception e)

8
Throw
  • Allows the programmer to handle the exception
    within the area and then sends it up to the next
    level to handle the exception
  • try
  • catch (IOException e)
  • //write code handle exception
  • Throws e // pass it along

9
Throws
  • Keyword that allows other components of the
    program know that this code can throw an
    exception and it is not handled within the block
    of code
  • ..
  • class Test
  • public static void main(String args) throws
    IOException
  • BufferedReader in new BufferedReader(new
    InputReader(System.in))

10
finally
  • Keyword that is used to designate a block of code
    that should be executed whether there was an
    exception or there was no exception
  • try
  • .
  • catch (ArithmeticException e)
  • finally
  • //code will always be

11
Creating your own exceptions
  • Subclass Throwable
  • class yourClass extends Throwable
  • .
  • Exception would be called yourClass
  • Table 10-3.
Write a Comment
User Comments (0)
About PowerShow.com