3.2. Introduction

In Java, an exception is an event that occurs during the execution of a program that encounters an error or some kind of exceptional situation.

Note

Exceptions happen at runtime. Errors while compiling your programs are syntax errors - not exceptions.

While executing Java programs in the past, you have almost certainly run into a situation where your program crashed and output a message related to a runtime exception similar to the one below from our warm up exercise:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 9
     at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:48)
     at java.base/java.lang.String.charAt(String.java:1515)
     at Exceptions.main(Exceptions.java:15)

To avoid messages like this, you likely learned to surround blocks of code with conditional (if) statements so that certain lines of code only execute when the input is valid. In fact, this is a valid way to fix the code in the warm up exercise.

While avoiding exceptions using if statements is a valid approach, it is often tedious and error prone.

We will learn to think of exceptions as a special type of message sent from a method to alert its caller that something happened at runtime. If the programmer anticipates that something could go wrong, they can program a “backup plan” that will execute if/when the exception occurs allowing the program to recover after the exception occurs! More details soon.

In this chapter, you will see that it is almost always better to let the exception occur (let the offending method send the exception message back to the calling method) and let the calling method handle the exception (with its “backup plan”) than to try and avoid the exception.

We will demonstrate that exceptions, when used properly, can lead to cleaner code that has fewer bugs.

Important

Exceptions can be both informative and useful and should be handled instead of avoided. They can be viewed as a special type of message sent from one method to another when things don’t go as expected. Methods that receive an exception have the opportunity to incorporate a backup plan that allows the program to continue execution without a crash. Up until now, your methods did not have a backup plan which is why exceptions ultimately led to a crash.

Rapid Fire Review
  1. What is an exception in Java?

    1. An error that occurs during compilation.

    2. An event that occurs during the execution of a program when an error or exceptional situation is encountered.

    3. A message sent from a calling method to a called method.

    4. A type of bug that can always be avoided with if statements.

  2. According to the text, what is the key difference between an exception and a syntax error?

    1. Exceptions occur during compilation, while syntax errors occur at runtime.

    2. Exceptions are always avoidable, while syntax errors are not.

    3. Exceptions happen at runtime, while syntax errors occur during compilation.

    4. Syntax errors are a special type of message, while exceptions are not.

  3. While of the following scenarios would most likely lead to a runtime exception?

    1. Forgetting a semicolon at the end of a statement.

    2. Trying to divide a number by zero.

    3. Misspelling a variable name.

    4. Using an incorrect keyword in a loop.

  4. What is the preferred approach for dealing with exceptions, according to the text?

    1. Always avoid exceptions using if statements to prevent them from occurring.

    2. Let the exception occur and allow the calling method to handle it.

    3. Ignore exceptions, as they are not a bad thing.

    4. Rewrite the entire program to eliminate any possibility of errors.

  5. What is the primary benefit of handling exceptions?

    1. It makes the code longer and more complex.

    2. It allows the program to continue running without a crash when unexpected situations arise.

    3. It prevents all types of errors from ever happening in the program.

    4. It converts runtime errors into compile-time errors.