Exceptions - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Exceptions

Description:

Good-quality software should be able to cope with ... The preceding code has further drawbacks ... Unchecked exceptions are ones you must be prepared for ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 16
Provided by: scie246
Category:

less

Transcript and Presenter's Notes

Title: Exceptions


1
Exceptions
  • 11-07-05

2
Exception
  • Conveys the idea that something has gone wrong
  • Good-quality software should be able to cope with
    predictable problems
  • Examples
  • Loading a file that doesnt exist
  • Trying to print on a printer with no paper

3
If-then construct
  • if (something wrong)
  • handle the problem
  • else
  • handle the normal situation
  • DoA()
  • DoB()
  • DoC()

4
If-then
  • DoA()
  • If (DoA went wrong)
  • handle the DoA problem
  • Else
  • DoB()
  • if (DoB went wrong)
  • handle the DoB problem
  • else
  • DoC()
  • if (Doc went wrong)
  • hande the DoC problem
  • else
  • Etc.

5
If-then
  • The preceding code has further drawbacks
  • Methods can have many input parameters but can
    return only a single result
  • How to we signal a problem?
  • Negative numbers?
  • Zero-length string?
  • Such an approach is not general

6
Exceptions and Objects
  • Java is OO
  • Exceptions are an object
  • Use new to create an instance of the appropriate
    exception class
  • Another region of the program can then check for
    its existence

7
When to use exceptions
  • They provide a kind of control structure for
    unlikely events
  • Not to be used for normal bad things
  • User inputting a number of out range
  • Use exception handling facilities for errors
    rather than predictable normal cases

8
Jargon of Exceptions
  • Exceptions are indicated by being thrown
  • Exceptions are detected by being caught
  • Java uses these keywords
  • Throws
  • Throw
  • Try
  • Catch

9
Try-Catch example
  • See ExceptionDemo1.Java

10
Try and Scope
  • When a try block produces an exception, its
    execution is halted
  • Any variables declared within the try block
    become inaccessible
  • They cant be used in the catch block
  • Any variables that are required inside the catch
    block must be declared outside the try block

11
Search for a catcher
  • Method A invokes Method B which invokes Method C
  • Exception occurs in C
  • Looks in Method C
  • If found handles the exception there
  • Looks in Method B
  • Looks in Method A

12
Throwing -- an Introduction
  • Public static int parseInt(String s)
  • throws NumberFormatException
  • // code for method
  • throw new NumberFormatException()
  • // more code
  • // end of parseInt

13
Exception Classes
  • To catch we need a method to handle the
    appropriate exception
  • See figure 1 for different kinds of exceptions
  • Unchecked exceptions are ones you must be
    prepared for
  • Compiler will look for the other kind of
    exceptions (checked)

14
Using the exception class structure
  • try
  • // some code
  • catch (NumberFormatException e)
  • // handle number format problem
  • catch (IllegalArguementException e)
  • // hande this
  • catch (Exception e)
  • // handle this type of exception here

15
Finally
  • try
  • while (line myFile.readlin() ) ! null)
  • // process line
  • catch (IOexception e)
  • errorField.setText(Error in file input)
  • finally
  • myFile.close()
Write a Comment
User Comments (0)
About PowerShow.com