Exceptions - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Exceptions

Description:

NullPointerException - method called in try, was supposed to return an object ... AWTException - method called in try, attached a handler object that was ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 16
Provided by: cseBu
Category:
Tags: exceptions | try

less

Transcript and Presenter's Notes

Title: Exceptions


1
Exceptions
  • Protecting methods from errors
  • CATCHING an exception protects your program from
    bad users
  • THROWING an exception protects others who use
    your methods

2
catching an error
  • try
  • x Integer.parseInt( userString )
  • catch (Exception e)
  • System.out.println(Not an integer!)

3
trycatch
  • Tries the try-bracketed statement.
  • try
  • // whatever is here
  • Stops at an error. Does not complete.
  • Error info is placed into the object e, of the
    Exception class - can decipher ALL exceptions
  • catch (Exception e)
  • // do something here

4
Exception class
  • catch (Exception e)
  • declares an object e of the Exception class
  • there are methods to be used
  • catch (Exception e)
  • e.printStackTrace( ) // where the error was
  • System.out.println(user did e.getMessage(
    ) )
  • // what input
    caused error
  • System.out.println(computer thought
    e.toString( ) )
  • // the
    computers description of
  • // the
    message

5
Summary - Exception methods( )
  • catch (Exception e)
  • e.getMessage( ) - what the user typed that
    generated the error
  • e.toString( ) - the computers description of the
    message
  • e.printStackTrace( ) - list all the methods from
    main, to the one that generated the error

6
catch different types
  • try
  • myClass myObject new myClass()
  • myObject.myMethod( )
  • // method 2( )
  • // method 3( )
  • catch (NullPointerException e)
  • catch (DivideByZeroException e)
  • System.out.println(Cant divide by zero)

7
different types
  • NullPointerException - method called in try, was
    supposed to return an object (like Color), but
    didnt (user hit cancel in JColorChooser)
  • ArrayIndexOutOfBoundsException - method called
    in try, accessed index greater than the array
    definition.

8
different types
  • NumberFormatException - method called in try,
    like parseInt, tried to convert a string to a
    number, and the string wasnt a digit.
  • ArithmeticException - method called in try, did
    something arithmetically wrong - like mixing
    variable types.

9
different types
  • AWTException - method called in try, attached a
    handler object that was inappropriate for the
    component (a MouseListener to a JButton, or an
    object not of an ActionListener class).
  • DivideByZeroException - method called in try, did
    a divide by zero.

10
throwing an exception
  • public myClass
  • public double myMethod( int q) throw
    DivideByZeroException
  • if (q 0)
  • throw new DivideByZeroException( )
  • else
  • return(10/q)

11
User Defined Exceptions
12
1. extending Exception
  • class HighTempException extends Exception
  • public HighTempException()
  • super(Temperature Exceeds Safe Level")

13
super gives us all the necessary methods
  • getMessage( ) - what the user typed that
    generated the error
  • toString( ) - the computers description of the
    message
  • printStackTrace( ) - list all the methods from
    main, to the one that generated the error

14
2. throwing
  • public void increasePowerLevel() throws
    highTempException
  • powerLevel powerLevel 10
  • temperature powerLevel 100
  • if (temperature gt 10000)
  • throw new highTempException()
  • // end increasePower

15
3. using (i.e. catching)
  • try
  • chernobyl.increasePowerLevel()
  • catch (highTempException hte)
  • hte.printStackTrace()
  • System.exit(0)
Write a Comment
User Comments (0)
About PowerShow.com