The Throwable Class Hierarchy


Introduction

Exceptions are used in Java to indicate that an exceptional condition has occurred during program execution. You have been informally exposed to exceptions since chapter 3. The code examples presented thus far have minimized the need to explicitly handle exceptions. However, Java programs are always under the threat of throwing some kind of exception. (By exception I mean any class that extends the java.lang.Throwable class. Two direct subclasses of Throwable are the Error and Exception classes, but I’m getting a little ahead of myself.)

Some types of exceptions need to be handled explicitly by the programmer. These require the use of try/catch blocks. Other types of exceptions, specifically exceptions that extend the Error class and exceptions that extend the RuntimeException class, can be thrown by the Java Virtual Machine during program execution. Any attempt to catch and handle all these exception conditions would quickly exhaust you.

This chapter dives deep into the explanation of exceptions to show you how to properly handle existing exceptions (i.e., those already defined by the Java API) and how to create your own custom exceptions. You will be formally introduced to the Throwable class, try/catch/finally blocks, and the throws clause. You will also learn the difference between checked and unchecked exceptions. After reading this chapter you will be well prepared to properly handle exception conditions in your programs. You will also know how to create your own custom exception classes to better fit your abstraction requirements.

Why is this chapter placed at this point in this book? The answer is simple: you will soon reach the chapters that cover topics like network and file I/O programming. In these two areas especially you will be required to explicitly handle many types of exception conditions. As you write increasingly complex programs you will come to rely on the information conveyed by a thrown exception to help you debug your code. Now is the time to dive deep into the topic of exceptions!




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net