Cup 12: Exceptions - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Cup 12: Exceptions

Description:

Try, Catch, and Finally. Each try block is followed by one or more catch blocks, and an optional finally block ... is executed inside the try or catch blocks! ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 12
Provided by: timma87
Category:
Tags: cup | exceptions | try

less

Transcript and Presenter's Notes

Title: Cup 12: Exceptions


1
Cup 12 Exceptions
  • Special Topics Java

Dr. Tim Margush Department of Mathematics and
Computer Science The University of Akron
2
What is an Exception
  • An Object (of class Exception) created when a
    situation occurs disrupting the normal flow of
    execution
  • Instance variables store information about the
    exception
  • Exceptions are thrown to a block of code designed
    to catch them

3
What Causes an Exception
  • Runtime errors in your program
  • Invalid type cast
  • Array index out of bounds
  • Division by zero
  • Throw statement
  • Allows program to create an exception
  • Standard Method Exceptions
  • IOException
  • MalformedURLException
  • StringIndexOutOfBoundsException
  • Java Errors
  • Out of memory
  • Missing class definition
  • Bytecode error

4
Class Hierarchy
  • Throwable extends Object
  • Error extends Throwable
  • You generally do not catch these objects
  • Exception extends Throwable
  • RuntimeException
  • Arithmetic, cast, array index, illegal arguments,
    null pointer, security
  • IOException
  • EOF, MalformedURL, InterruptedIO
  • Many, many others!!!

5
Checked Exceptions
  • Runtime exceptions are considered too costly to
    require catching
  • It is your responsibility to ensure Runtime
    Exceptions will not occur
  • You may catch Runtime Exceptions
  • Other Exceptions are called Checked Exceptions
  • You are required to deal with these in your code

6
Dealing With Exceptions
  • Catch It!
  • try
  • statements that might throw one or more
    exceptions
  • catch (SomeException e)
  • code to handle the exception
  • catch (OtherException e)
  • code to handle the other exception
  • Pass the Buck!
  • int X()throws IOException
  • statements that might throw an IOException
  • This makes the Exception part of the method's
    interface
  • The calling method must then deal with the
    Exception

7
Try, Catch, and Finally
  • Each try block is followed by one or more catch
    blocks, and an optional finally block
  • finally
  • code to be executed after the try, regardless of
    the try block's outcome - even if a return is
    executed inside the try or catch blocks!

8
Re-Throwing an Exception
  • try
  • ...
  • catch (Exception e)
  • do some things
  • throw e
  • This will cause the caught exception to be
    rethrown
  • This method will need to declare that it might
    throw the Exception
  • The calling method will also have to deal with it.

9
Creating Your Own Exceptions
  • You will need to define your own Exception class
    to extent Throwable
  • public class YourException extends Throwable
  • public YourException()
  • public YourException(String s)
  • super(s)
  • //other methods???

10
Throwable?
  • class Throwable contains 4 public methods
  • getMessage()
  • Includes name of exception class and message
    stored in exception object
  • printStackTrace()
  • printStackTrace(PrintStream s)
  • displays message and traceback through stack
  • toString()

11
Throwing Your Own Exceptions
  • throw new YourException("Custom")
  • This is a checked exception, so it will either
    have to be caught, or declared in the throws
    clause of the method
Write a Comment
User Comments (0)
About PowerShow.com