i

JAVA Complete Course

Custom Exceptions

We have covered almost every use case of exceptions handling in java. In java we also can have our custom exceptions. Recommendation is that don’t create our exceptions as checked exceptions. Creating our custom exceptions is easy, we just need to extend any exception class (For example: Exception, RuntimeException, Throwable, etc.). Then we can have few additional properties or customization of messages of exceptions for our concrete exception class.

Let’s return to our well known zero division example. As in Java we don’t have DivideByZeroException, we can create such kind of exception class from Throwable and then if we detect division by zero, we can throw this exception.

A screenshot of a cell phone

Description automatically generated