Docsity

Java Exception Handling: Understanding Exceptions and Their Basics, Slides of Object Oriented Programming

Partial preview of the text

Download Java Exception Handling: Understanding Exceptions and Their Basics and more Slides Object Oriented Programming in PDF only on Docsity!

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 in 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 (catch it), or - Register that you method may throw that exception

4

Basics of Exceptions (cont)

  • You can use two method from the Throwable

class, the parent of Exception, getMessage() and printStackTrace()

  • Ex: (2) TryBlockTest2.java
  • You can define your own exception class which

should (in almost all cases) inherit from Exception or one of its children.

  • You can throw the exception after catching it
  • Example of a defined one: TryBlockTest