Get startedGet started for free

Throwing

1. Throwing

We've seen how to handle exceptions with try/catch. Let's look at the alternative using throws.

2. Handling options

As we have learned, checked exceptions require "handling", while handling is optional for `RuntimeException`. We've also learned that one way to handle an exception is with a try-catch. But there is a second way to handle an exception - we can `throw` the exception. Usually, we prefer the try-catch approach, but there are times when throwing is helpful.

3. Throws keyword

We add the `throws` keyword and a list of exception types to any method where the method will not handle an exception with try-catch. This causes the exception to be given to the calling method - in this example any method calling "someMethod". We call this "passing the buck" because we are avoiding responsibility for the exception handling - and are passing the exception back to the method caller where we let it decide how to fix the problem. Any number of exception types can be thrown by using a comma separated list of `Exception` types. In this example, the caller of `someMethod()` might be thrown IndexOutOfBoundsException, NegativeArraySizeException, or a NullPointerException.

4. Throws example

Let's see how `throws` works. On the left, we have an example of using try-catch to handle an `Exception` - in this case try/catch of `IndexOutOfBoundsException`. On the right, we use `throws`. Notice how `.someMethod()` on the right doesn't contain any try/catch `Exception` handling code. Instead, it uses `throws` with the `IndexOutOfBoundsException` type to tell any calling method that it should handle the exception. In this case, the main method, as the caller of `someMethod()` has the try/catch to handle the `IndexOutOfBoundsException`. Using throws is like saying to the world - "hey, the exception type could happen here, but its up to the caller to take care of it". We eventually need to try/catch somewhere or else the application fails. Using throws delays the immediate handling of the exception in the method where it was caused.

5. When to use throws

Remember, with a checked exception, we have to use either try/catch or throws to handle the exception. For runtime exceptions, use of try/catch or throws are optional. In general, it is considered best practice in Java to handle exceptions in the method in which they happen. However, there may be times when using throws is more appropriate. Sometimes a method may not know how to recover. This might be the case if a method doesn't have the information or resources to deal with the exception. Another case may be when we want to centralize the handling of exceptions caused in calling many methods thereby reducing try/catch blocks.

6. Catch and rethrow

An `Exception` can be passed from method caller to method caller. This means a method calling another method that throws an exception can also throw (known as "rethrow") rather than handling the exception with try/catch. We'll see an example of rethrowing in a second. If main rethrows an exception thrown to it, the application will halt and display the exception message in the console.

7. Rethrowing example

Here's an example of rethrowing an exception. `method2()` is where an ArithmeticException occurs as we try to divide by zero. Rather than handling the exception with try/catch, `method2()` throws its exception to `method1()`. But rather than try/catch the exception, `method1()` also rethrows ArithmeticException to main where it is handled.

8. Let's practice!

That was a lot! Let's throw some exceptions.