Chapter 7: Java Exception Handling - PowerPoint PPT Presentation

1 / 4
About This Presentation
Title:

Chapter 7: Java Exception Handling

Description:

An Exception usually signals an error in your program. ... You can use two method from the Throwable class, the parent of Eception, ... – PowerPoint PPT presentation

Number of Views:363
Avg rating:3.0/5.0
Slides: 5
Provided by: far71
Category:

less

Transcript and Presenter's Notes

Title: Chapter 7: Java Exception Handling


1
Chapter 7 Java Exception Handling
2
Basics of Exceptions
  • An Exception usually signals an error in your
    program.
  • You need to handle exception only is unusual or
    catastrophic situations
  • Commonly happened situations like input errors
    should be handled by your code
  • Most of the exception classes inherit from the
    Java class Exception
  • If a method has the potential of generating an
    exception you must either
  • Deal with it in your code, or
  • Register that you method may throw that exception

3
Basics of Exceptions (cont)
  • Subclasses of RunTimeException, a subclass of
    Exception, are exempted from the previous
    requirement
  • Ex ArithmeticException, ClassCastException, ..
  • Specifying a throwable exception
  • Double myMethod throws IOExecption
  • Dealing with exception in your code (Try-catch
    block)
  • Try
  • you code
  • catch ( Exception e )
  • Ex TestTryCatch, TestLoopTryCatch,
    TestLoopTryCatch2
  • You can have more than one catch block
  • In addition, you can use a finally block at the
    end of all catch blocks. This will be always
    executed
  • Ex TryBlockTest.java

4
Basics of Exceptions (cont)
  • You can use two method from the Throwable class,
    the parent of Eception, getMessage() and
    printStackTrace()
  • Ex (2) TryBlockTest.java
  • You can define your own exception class which
    should (in almost all cases) inherit from
    Exception or one of its children. Ex
    DreadfulProblemException.java
  • You can throw the exception after catching it
  • Example of a define one define your own
Write a Comment
User Comments (0)
About PowerShow.com