Chapter 7. Exception handling

Exam objectives covered in this chapter

What you need to know

[8.3] Describe the advantages of Exception handling. The need for and advantages of exception handlers.
[8.1] Differentiate among checked exceptions, unchecked exceptions, and Errors. Differences and similarities between checked exceptions, RuntimeExceptions, and Errors. Differences and similarities in the way these exceptions and errors are handled in code.
[8.2] Create a try-catch block and determine how exceptions alter normal program flow. How to create a try-catch-finally block. Understand the flow of code when the enclosed code throws an exception or error. How to create nested try-catch-finally blocks.
[8.4] Create and invoke a method that throws an exception. How to create methods that throw exceptions. Rules that cover when overriding or overridden methods throw or don’t throw exceptions. How to determine the flow of control when an invoked method throws an exception. How to apply this to cases when one is thrown without a try block and from a try block (with appropriate and insufficient exception handlers). The difference in calling methods that throw or don’t throw exceptions.
[8.5] Recognize common exception classes (such as NullPointerException, Arithmetic-Exception, ArrayIndexOutOfBounds-Exception, ClassCastException) How to recognize the code that can throw these exceptions and handle them appropriately.

Imagine you’re about to board an airplane to Geneva to attend an important conference. At the last minute, you learn that the flight has been cancelled because the pilot isn’t feeling well. Fortunately, the airline quickly arranges for an alternative pilot, allowing the flight to take off at its originally scheduled time. What a relief!

This example illustrates how exceptional conditions can modify the initial flow of an action and demonstrates the need to handle those conditions appropriately. In Java, an exceptional condition (like the illness of a pilot) can affect the normal code flow (airline flight operation). In this context, the arrangement for an alternative pilot can be compared to an exception handler.

Depending on the nature of the exceptional condition, you may or may not be able to recover completely. For example, would airline management have been able to get your flight off the ground if, instead, an earthquake had damaged much of the airport?

In the exam, you’ll be asked similar questions with respect to Java code and exceptions. With that in mind, this chapter covers the following:

  • Understanding and identifying exceptions arising in code
  • Determining how exceptions alter the normal program flow
  • Understanding the need to handle exceptions separately in your code
  • Using try-catch-finally blocks to handle exceptions
  • Differentiating between checked exceptions, unchecked exceptions, and errors
  • Invoking methods that may throw exceptions
  • Recognizing common exception categories and classes

You might feel like we’re covering a lot in this chapter, but remember that we aren’t going to delve into too much background information because I assume you already know the definitions and uses of classes and methods, class inheritance, arrays, and ArrayLists. Our focus in this chapter is on the exam objectives and what you need to know about exceptions.

In this chapter, I won’t discuss a try statement with multiple catch clauses, automatic closing of resources with a try-with-resources statement, or the creation of custom exceptions. These topics are covered in the next level of Java certification (in the OCP Java SE 8 Programmer II exam).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.204.186