From the course: Java 8 Essential Training

Unlock the full course today

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

Throwing custom exceptions

Throwing custom exceptions - Java Tutorial

From the course: Java 8 Essential Training

Start my 1-month free trial

Throwing custom exceptions

- I've described how to handle exceptions that are thrown by the Java Runtime and its core libraries. But you can also generate your own exceptions, and give them custom error messages. I'm working on a project named Throw, and I'm starting with code similar to what I had in the last one. I've declared an array of characters, and then within a try block I try to get a value from the characters' array. If I survive that, I then try to get a value from a sub-string. In either case, I could throw an error, and I have two catch blocks to handle the different errors. One for an array index out of bounds exception, and one for a string index out of bounds exception. If you want to throw your own exceptions, you can either use the core exception class, or create your own custom exception classes. I'm going to stick to the core exception class here, and I'm going to add some code within the try block. I'll add a bit of conditional code. I'll start with the key word if, and then I'll set my…

Contents