Exception Handling - PowerPoint PPT Presentation

Loading...

PPT – Exception Handling PowerPoint presentation | free to download - id: 7091a6-MmQwY



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Exception Handling

Description:

Exception Handling What is an exception? Why use exception handling? How else can we handle errors? When to use exception handling? How do we do exception handling? – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 13
Provided by: CIS494
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Exception Handling


1
Exception Handling
  • What is an exception?
  • Why use exception handling?
  • How else can we handle errors?
  • When to use exception handling?
  • How do we do exception handling?

2
An exception is
  • an indication that a problem occurred during the
    programs execution.

3
Exception handling
  • enables programmers to write clearer, more
    robust, more fault-tolerant programs.

4
Typical error handling
  • If statements at point where error might occur
  • Handling is embedded throughout the code
  • Is messy and clutters up the code
  • Is not reusable
  • Cannot always be done

5
When to use exception handling
  • When a method would be unable to complete its
    process otherwise
  • When its inappropriate for a method to handle
    the exception itself
  • For uniform exception handling throughout a big
    project

6
How do we do exception handling?
  • Try/catch blocks
  • Use exception classes
  • Throw exception objects
  • Throws exception clause
  • Finally block

7
Try/catch blocks
  • Put the code in the try block that might cause
    the problem. Set up corresponding catch blocks
    for each type of exception that might occur in
    the try block.

8
Example
  • try
  • number1 Integer.parseInt ( inputField.get
    Text() )
  • number2 Integer.parseInt (inputField.getText()
    )
  • result quotient (number1, number2 )
  • outputField.setText (result)
  • Catch (NumberFormatException numberFormatException
    )
  • JOptionPane.showMessageDialog (this, You must
    enter two integers, Invalid Number Format,
    JOptionPane.ERROR_MESSAGE)

9
Example of throwing an exception throws and
throw
  • Public double quotient (int numerator, int
    denominator) throws DivideByZeroException
  • if (denominator 0 )
  • throw new DivideByZeroException()
  • return (double) numerator / denominator

10
Class Hierarchy for Exceptions
  • Object
  • Throwable
  • Error Exception

11
A Sample of Exception and Error Classes
  • Error (no control over these)
  • LinkageError
  • ThreadDeath
  • VirtualMachineError
  • Exception (can use these and extend them)
  • ArithmeticExceptin
  • ClassNotFoundException
  • IOException
  • NullPointerException
  • IndexOutOfBoundsException

12
finally block
  • Use to put away resources, close files, etc.
About PowerShow.com