Outline - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Outline

Description:

... the way up to main(); if not caught there, the JVM exits with an error ... If the exception is not caught, the finally clause is executed before propogating up ... – PowerPoint PPT presentation

Number of Views:57
Avg rating:3.0/5.0
Slides: 25
Provided by: cs11
Category:
Tags: caught | outline

less

Transcript and Presenter's Notes

Title: Outline


1
Outline
  • Exceptions
  • Exception class hierarchy
  • Exception control flow
  • throwing exceptions
  • try/catch statements
  • finally
  • throws clauses

2
Recap
  • Advanced GUI Programming
  • Window creation- JFrames
  • Menu use- JMenu, JMenuBar, JMenuItem
  • Event Handling

3
Exceptions
  • Something that while not unexpected, is generally
    undesirable for the purposes of the program
  • E.g.,
  • user enters invalid filename
  • a network link fails
  • user enters a word when prompted for a number

4
Example Exceptions
  • Divide by zero
  • Access array elements beyond the end of the array
  • Try to call a method from an object reference
    that is null

5
Terminology
  • When an exception is discovered at runtime, it is
    said to be thrown
  • Attempting to anticipate when an exception may be
    thrown, and work around it, is known as trying to
    catch the exception
  • Successfully catching an exception and working
    around it is known as handling the exception

6
Class Hierarchy
Errors (serious problems) like OutOfMemoryError
Checked Exceptions (can occur even during
execution of good code) like FileNotFoundExceptio
n
7
Throwable
  • The superclass of all errors and exceptions in
    the Java language
  • Only objects of this class or its subclasses can
    be used with the Java throw statement or caught
    in a catch clause

8
Error
  • Subclass of Throwable indicating serious problems
    that a reasonable application need not try to
    catch.
  • E.g., out of memory

9
Exception
  • Conditions that a reasonable program may want to
    catch
  • Applications often define their own Exception
    subclasses
  • E.g., SingularMatrixException in a math program

10
Exception Control Flow
  • When an exception is thrown
  • The current block is immediately exited
  • If the block has a related catch clause, the
    catch clause is entered and executed
  • Execution then continues after the catch clause
  • If not, the enclosing block (or method!) is
    exited and checked for a catch clause
  • And so on, all the way up to main() if not
    caught there, the JVM exits with an error message.

11
Throwing Exceptions
  • Most often, exceptions will be thrown by the
    class library however, you may define and throw
    your own exceptions too.
  • Syntax throws
  • E.g., throws IOException

12
Try/catch Clauses
  • Syntaxtry statement/block that may
    throw exception(s)catch(ExceptionClass1
    e) catch block 1catch(ExceptionClass2 e) catch
    block 2

13
Notes on try/catch
  • Each catch clause handles a different Exception
    subclass
  • Catch clauses are tested in order (so if
    Exception were first, it would catch all
    exceptions, and any other catch clauses would be
    ignored)
  • The exception instance is an argument to the
    catch clause

14
finally
  • A try/catch may have one optional finally
    clausetry ... catch(Exception e) ...
    finally ...
  • Code in a finally block is executed regardless if
    an exception is thrown or not
  • If an exception is caught in the try/catch, it
    will be executed after the catch clause
  • If the exception is not caught, the finally
    clause is executed before propogating up

15
try/catch Example
  • try
  • // some code
  • catch(NumberFormatException e)
  • System.out.println(Please enter a number
    only!)
  • catch(DivideByZeroException e)
  • System.out.println(Please do not enter zero!)
  • finally
  • System.out.println(An exception may or may not
    have occurred)

16
throwing Exceptions
  • If the current method/block isnt a good place to
    handle the exception, it can be thrown up to the
    caller
  • However, the method must declare that it can
    throw such exceptions
  • This lets the caller know that calling that
    method may cause an exception to occur, so they
    can decide whether to write a catch clause for it
    properly, or to throw it up themselves

17
Methods throwing Exceptions
  • Syntax throws class 1,
  • E.g.,public String getInput() throws
    IOException BufferedReader reader new
    BufferedReader(newInputStreamReader(System.in))
    return reader.readLine()

18
Checked Exceptions
  • Any exception not a subclass of RuntimeException
    is a checked exception
  • Any statement which could possibly cause a
    checked exception must either be
  • caught in a try/catch clause, or
  • declared in a throws clause of the enclosing
    method
  • RuntimeExceptions are not checked, in order to
    prevent overly cumbersome code

19
Exceptions Example
  • // The exception class
  • public class NegativeSquareRootException
  • extends Exception
  • public NegativeSquareRootException()
  • super(Attempted to take square root of
    negative number)

20
Exceptions Example, contd
  • public class SquareRoot
  • public static double squareRoot(double value)
  • throws NegativeSquareRootException
  • if ( value
  • throw new NegativeSquareRootException()
  • return Math.sqrt(value)

21
Exceptions Example, contd
  • import java.io.
  • public class ExceptionTest
  • public static void main(String args) throws
    IOException
  • BufferedReader reader
  • new BufferedReader(new InputStreamReader(Sys
    tem.in))
  • try
  • String input reader.readLine()
  • double d Double.parseDouble(input)
  • double sqrt SquareRoot.squareRoot(d)

22
Exceptions Example, contd
  • catch(NumberFormatException e)
  • System.out.println(Please enter a number
    only)
  • catch(NegativeSquareRootException e)
  • System.out.println(e.getMessage())
  • System.out.println(Can only sqrt positive
    numbers)
  • finally
  • System.out.println(Thanks for using my
    program!)

23
Summary
  • Use exceptions to handle complicated error
    conditions in your programs
  • The exception class can contain as much data as
    you like!
  • Checked exceptions must be caught or declared as
    thrown
  • RuntimeExceptions may be caught, but need not be
    declared as thrown

24
Notes
  • MP5 due next week
  • Final quiz next week
  • OOP (e.g. inheritance, polymorphism)
  • GUI
  • Exception handling
Write a Comment
User Comments (0)
About PowerShow.com