Exceptions - PowerPoint PPT Presentation

1 / 48
About This Presentation
Title:

Exceptions

Description:

Old School. have the method return an indication of success/failure (an 'error code' ... New School ... out.println('How old are you?'); String inputLine ... – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 49
Provided by: thaddeusf
Category:
Tags: exceptions | old | school

less

Transcript and Presenter's Notes

Title: Exceptions


1
Exceptions
  • CSC 171 FALL 2004
  • LECTURE 24

2
READING
  • Read Horstmann Chapter 14
  • This course covered Horstmann Chapters 1 - 15

3
EXAM
  • Thursday 12/9 in class
  • Chapters 11, 13-15

4
Make up exam
  • Friday 12/3 1215PM-125PM
  • CSB 703

5
GRADING
  • OLD
  • Midterm 10
  • Projects (4) 40
  • Final 15
  • Quizes (10) 10
  • Labs (15) 15
  • Workshops 10
  • NEW
  • Exam 1 13
  • Projects (4) 40
  • Exam 2 12
  • Quizes (6) 5
  • Labs (21) 20
  • Workshops 10

6
Errors in Programming
7
Errors in Programming
  • Sometimes cause by our code
  • Sometimes caused by external code
  • Reasonable to take precautions

8
Two aspects of errors
  • Recognizing when an error occurs
  • Dealing with the errors

9
  • There are two aspects of handling failure
    ________________ and ________________.

10
  • There are two aspects of handling failure
    ___detection____ and ____recovery____________.

11
Old School
  • have the method return an indication of
    success/failure (an error code)
  • x.doSomething()
  • // becomes
  • if (x.doSomething() -1) return false

12
New School
  • Java has an exception handling mechanism which
    can require potential errors to be recognized.
  • This mechanism is flexible and efficient.

13
  • In Java, ___________________________ provides a
    flexible mechanism for passing control from the
    point of error detection to a recovery handler.

14
  • In Java, __exception handling_______ provides a
    flexible mechanism for passing control from the
    point of error detection to a recovery handler.

15
Exception Objects
16
THROWING EXCEPTIONS
  • If I am a method
  • AND If a problem occurs
  • then I can deal with it by
  • Constructing a new exception object describing
    the problem
  • Throw the object to the method that invoked me
    (I have to terminate to do this)

17
Example
  • public class BankAccount public void
    withdraw(double amount) if (amount gt
    balance) throw new IllegalArgumentException(
    "Amount exceeds balance") balance
    balance - amount ...

18
  • To signal an exceptional condition, use the
    _____________ statement to throw an
    _____________________ object.

19
  • To signal an exceptional condition, use the
    ____throw____ statement to throw an
    __________exception____ object.

20
  • When you throw an exception, the current method
    _____________________.

21
  • When you throw an exception, the current method
    __terminates______.

22
CHECKED/UNCHECKED
  • Its hard to anticipate all possible exceptions
    at compile time.
  • The compiler checks to see if we deal with
    exceptions (checked exeptions)
  • Unchecked exceptions are not enforced by the
    compiler.

23
  • import java.io.BufferedReader
  • import java.io.InputStreamReader
  • import java.io.IOException
  • public class Console
  • public static void main(String args)
  • InputStreamReader isreader new
    InputStreamReader(System.in)
  • BufferedReader console new BufferedReader(isrea
    der)
  • String input1 console.readLine()
  • System.out.println(input1)

24
  • import java.io.BufferedReader
  • import java.io.InputStreamReader
  • import java.io.IOException
  • public class Console
  • public static void main(String args) throws
    IOException
  • InputStreamReader isreader new
    InputStreamReader(System.in)
  • BufferedReader console new BufferedReader(isrea
    der)
  • String input1 console.readLine()
  • System.out.println(input1)

25
  • import java.io.BufferedReader
  • import java.io.InputStreamReader
  • import java.io.IOException
  • public class Console
  • public static void main(String args)
  • InputStreamReader isreader new
    InputStreamReader(System.in)
  • BufferedReader console new BufferedReader(isrea
    der)
  • try
  • String input1 console.readLine()
  • System.out.println(input1)
  • catch (IOException e)
  • System.out.println(Problem)

26
Unchecked Exceptions
  • int k Integer.parseInt(Hello World)
  • int a 2,3,4,5,6,7,8,9
  • a20 5

27
  • There are two kind of exceptions
    ______________________ and _____________________
    exceptions.

28
  • There are two kind of exceptions
    ___checked_______ and ___unchecked________
    exceptions.

29
  • Unchecked exceptions extend the class
    _________________________ or _________________.

30
  • Unchecked exceptions extend the class
    _RuntimeException______ or _____Error______.

31
  • Checked exceptions are due to ____________________
    ______________. The compiler checks that your
    program handles these exceptions.

32
  • Checked exceptions are due to _external
    circumstances___. The compiler checks that your
    program handles these exceptions.

33
  • Add a _____________________ specifier to a method
    that can throw a checked exception.

34
  • Add a __throws_______ specifier to a method that
    can throw a checked exception.

35
(No Transcript)
36
  • You can design your own exception types
    subclasses of _______________________ or
    ______________________.

37
  • You can design your own exception types
    subclasses of __Exception____ or
    ___RuntimeException_.

38
  • public class InsufficientFundsException
  • extends RuntimeException
  • public InsufficientFundsException()
     public InsufficientFundsException(String
    reason) super(reason)

39
TRY/CATCH
  • Statements in try block are executed
  • If no exceptions occur, catch clauses are skipped
  • If exception of matching type occurs, execution
    jumps to catch clause
  • If exception of another type occurs, it is thrown
    to the calling method
  • If main doesn't catch an exception, the program
    terminates with a stack trace

40
  • try BufferedReader in new BufferedReader(
    new InputStreamReader(System.in))
    System.out.println("How old are you?") String
    inputLine in.readLine() int age
    Integer.parseInt(inputLine) age
    System.out.println("Next year,you'll be "
    age)
  • catch (IOException exception)
    System.out.println("Input/output error "
    exception)
  • catch (NumberFormatException exception)
    System.out.println("Input was not a number")

41
  • In a method that is ready to handle a particular
    exception type, place the statements that can
    cause the exception inside a ____________________,
    place the handler inside a ______________________
    ________.

42
  • In a method that is ready to handle a particular
    exception type, place the statements that can
    cause the exception inside a ________try
    block________, place the handler inside a __catch
    clause_________.

43
  • It is better to _________________________________
    than to _______________________.

44
  • It is better to ________give_______________ than
    to ________ receive _________.

45
finally
  • Exception terminates current method
  • Danger Can skip over essential code
  • Example BufferedReader in in new
    BufferedReader(   new FileReader(filename))
    purse.read(in) in.close() 
  • Must execute in.close() even if exception happens
  • Use finally clause for code that must be executed
    "no matter what"

46
  • Once a try block is entered, the statements in a
    ___________________ clause are guaranteed to be
    executed, whether or not an exception is thrown.

47
  • Once a try block is entered, the statements in a
    ______ finally _____ clause are guaranteed to be
    executed, whether or not an exception is thrown.

48
Example
  • BufferedReader in null try in new
    BufferedReader( new FileReader(filename))
    purse.read(in) finally if
    (in !null) in.close()
Write a Comment
User Comments (0)
About PowerShow.com