exceptions and exception handling n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Exceptions and Exception Handling PowerPoint Presentation
Download Presentation
Exceptions and Exception Handling

Loading in 2 Seconds...

play fullscreen
1 / 12

Exceptions and Exception Handling - PowerPoint PPT Presentation


  • 177 Views
  • Uploaded on

Exceptions and Exception Handling. Carl Alphonce CSE116. Topic overview. Exceptions and exception handling What are exceptions? When should you use exceptions? Details How are exceptions generated (thrown)? How are exceptions handled (caught)? Runtime exceptions vs. other exceptions .

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Exceptions and Exception Handling' - takoda


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
topic overview
Topic overview
  • Exceptions and exception handling
    • What are exceptions?
    • When should you use exceptions?
    • Details
      • How are exceptions generated (thrown)?
      • How are exceptions handled (caught)?
      • Runtime exceptions vs. other exceptions.

Intermediate Java

what are exceptions
What are exceptions?
  • Exceptions are a mechanism for handling “exceptional” situations which can occur at runtime.
  • Many languages provide exceptions.
  • Terminology:
    • code where something unexpected happens “throws” an exception
    • code which handles the exceptional situation “catches” the exception – this code is called an exception handler

Intermediate Java

when are exceptions appropriate
When are exceptions appropriate?
  • Exceptions are appropriate to use to signal to a caller a problematic situation which cannot be handled locally.
  • Example: Consider a file reading component which is used both by an interactive UI and a batch processing component. If the file reading component has a problem (e.g. “disk is full”), can it decide locally how to respond? No. It is up to the client to decide how to react. The UI may notify its human user. The batch processor may log the problem, and skip processing of this file.

Intermediate Java

when are exceptions not appropriate
When are exceptions not appropriate?
  • It is not appropriate to use the exception mechanism when an exceptional situation can be handled locally.
  • It is not appropriate to use the exception mechanism in dealing with situations which are not exceptional.
    • If a particular situation is expected, it should be explicitly checked for.
    • For example, if a user supplies the name of a file to be read, it is better to check for existence of the file rather than to attempt to read and rely on a thrown exception to give notice if the file doesn’t exist.

Intermediate Java

how are exceptions generated
How are exceptions generated?
  • An exception is an object
  • An exception must derive from the java.lang.Exception class
  • Detour into inheritance and typing…

Intermediate Java

types and subtypes
Types and subtypes
  • Every class in Java defines a type.
  • Every interface in Java defines a type.
  • Types are arranged into a hierarchy:
    • classes can extend classes;
    • interfaces can extends interfaces;
    • classes can implement interfaces.
  • Every class except Object has a parent class (which is Object if no other parent is given): every other class has exactly one parent.

Intermediate Java

hierarchy for exceptions partial
Hierarchy for Exceptions (partial)
  • Object
    • Throwable
      • Error
        • LinkageError
        • ThreadDeath
        • VirtualMachineError
      • Exception
        • IOException
          • FileNotFoundException
          • MalformedURLException
        • RuntimeException
          • IndexOutOfBoundsException
          • NullPointerException

Intermediate Java

significance of hierarchy
Significance of hierarchy
  • The type hierarchy is significant, not only for exceptions, but for typing in Java more generally.
  • A variable declared to be of a given type can be assigned an object of that type, or of any subtype.
  • We make a distinction between
    • the declared type of a variable, and
    • the actual type of the object assigned to it.

Intermediate Java

how are exceptions generated1
How are exceptions generated?
  • An exception is an object.
  • An exception must derive from the java.lang.Exception class.
  • An exception object must be instantiated from an exception class.
    • new IndexOutOfBoundsException()
  • An exception must be thrown:
    • throw new IndexOutOfBoundsException()

Intermediate Java

what happens when an exception is thrown
What happens when an exception is thrown?
  • The exception is thrown until one of two things happens:
    • an exception handler for the thrown exception is found, or
    • the exception is uncaught (which typically results in program termination).
  • More technically, the runtime stack is unwound until a handler is found or the stack is empty.

Intermediate Java

runtime stack
Runtime stack?
  • Every time a method is called, an invocation record is pushed onto the runtime stack.
  • An invocation record stores things like:
    • parameter values
    • local variables
    • return value
    • return location
  • When a method finishes, its corresponding invocation record is removed (“popped”) from the runtime stack.

Intermediate Java