From the course: Java 11+ Essential Training

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Create multiple catch blocks

Create multiple catch blocks - Java Tutorial

From the course: Java 11+ Essential Training

Start my 1-month free trial

Create multiple catch blocks

- [Instructor] When you start working with structured exception handling, you quickly discover that not all exceptions are equal. The exception class is the super class for a whole bunch of different kinds of exceptions, and when you trigger an exception, you can find out very quickly what kind of exception you got. In this starting code, I'm going to encounter a null pointer exception, that's because I have an object that I've set as null, and then I'm trying to access one of its members that doesn't exist, and so when I run this code, I hit this catch block and I see the result of this printStackTrace call. Because I handled the exception, this code survives and I get to the last executable line, but you can use a structure called multi-catch where you have distinct catch blocks for each different kind of exception. Let's say for example that in this catch block, you wanted to make it specific to null pointers. In…

Contents