Exception Handling - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

Exception Handling

Description:

... is thrown in the try block, Java exits the try block and transfers control ... Advantages to Java's Exception Handling System ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 11
Provided by: chrisw53
Category:

less

Transcript and Presenter's Notes

Title: Exception Handling


1
Exception Handling
  • Exceptions are unexpected events
  • i/o errors
  • unintended inputs from users
  • divide by zero errors
  • Handling them appropriately helps us to achieve
    the robust quality.
  • Not handling them appropriately can cause program
    crashes or propagation of errors.

2
Throwing Exceptions
  • Here are some methods that we have used that can
    throw an exception.
  • public static float parseInt(String s) in the
    Integer class can throw a NumberFormat Exception
  • public static float parseFloat(String s) in the
    Float class can throw a NumberFormat Exception
  • public char charAt(int index) in the String class
    can throw an IndexOutOfBoundsException

3
Throwing Your Own Exceptions
  • try
  • if (n lt 0)
  • throw new Exception (Error Cant average 0
    items.)
  • catch (Exception e)
  • String str e.getMessage ()
  • ...

4
try Blocks
  • Convention says not to place every throw
    statement in its own try block. Include as many
    statements as possible.
  • If an exception is thrown, the try block is
    immediately exited and control does not return to
    it.
  • Do not put a non-void methods return statement
    inside a try block near the end. Why?
  • They must be followed immediately by one or more
    catch blocks.

5
try Block Example
  • try
  • float x Float.parseFloat(str1)
  • float y Float.parseFloat(str2)
  • if ( y 0 )
  • throw new ArithmeticException (y cannot be
    0)
  • System.out.println(We made it this far.)

variable scope?
6
catch Block
  • Catch blocks must be immediately preceded by a
    try block or another catch block.
  • Catch blocks have the general form
  • catch (ExceptionClassName myExc)
  • If an exception is thrown in the try block, Java
    exits the try block and transfers control to the
    first catch block that matches the particular
    kind of exception thrown.

7
Checked Exceptions
  • Checked Exceptions must either be caught within
    the method where it is thrown or declared within
    the method.
  • IOException and ClassNotFoundException are
    checked exceptions.
  • Any method that can throw an IOException that
    doesnt catch it must declare that it throws an
    IOException in its header.

8
  • import java.io.
  • public class Example
  • BufferedReader input new BufferedReader (new
    InputStreamReader(System.in))
  • public void doRead () throws IOException
  • String inputString input.readLine ()
  • public static void main (String argv throws
    IOException
  • Example ex new Example()
  • ex.doRead()

9
Unchecked Exceptions
  • Belong to some subclass of RunTimeException
  • Are not checked by the compiler.
  • Do not have to be handled within the program.
  • Do not have to be mentioned in the throws clause
    if they are not handled within a method.

10
Advantages to Javas Exception Handling System
  • Separates error handling code from regular
    code. The main logic of the program isnt lost in
    error handling code.
  • Propagates errors up the method call stack. Only
    the method that care about errors have to detect
    them.
  • Groups error types. Methods can be written to
    handle 1 or 2 types.
Write a Comment
User Comments (0)
About PowerShow.com