Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 6: Exceptions and Logging

C H A P T E R  6

image

Exceptions and Logging

Exceptions are a way of describing exceptional circumstances within a program. They are a way of signaling that something unexpected (exceptional) has happened. For that reason, exceptions are efficient at interrupting the current flow of the program and signaling that there is something that requires attention. As such, programs that judiciously use exceptions benefit from a better control flow and become more robust. Even so, using exceptions indiscriminately can cause performance degradation.

Within Java, exceptions are said to be thrown and caught. Throwing an exception involves telling the code that you have encountered an exception and involves using the throw keyword to signal the JVM to find any code capable of handling this exceptional circumstance within the current stack. Catching an exception involves telling the compiler which exceptions you can handle, and on which part of the code you want to monitor for these exceptions occurring. This is denoted by the try/catch Java syntax (described in recipe 6-1)


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial


 Â