How to work with Java Custom Exception

Java Exception has 2 types: CheckedException & UnCheckedException as discussion in Understand Java Exception & The effect to Java Program (Single & Multi Thread). The Java Custom Exception tutorial will introduce how to customize them by example.
Related Article:
How to handle Java Exception

I. Customize CheckException

As article Understand Java Exception & The effect to Java Program (Single & Multi Thread), CheckException extends from java.lang.Exception class. So for creating a custom CheckedException we create a class that extends java.lang.Exception.

Developers must handle CheckedException, in below code we use try/catch statement:

Result:

II. Customize UnCheckedException

As article Understand Java Exception & The effect to Java Program (Single & Multi Thread), UnCheckedException extends from java.lang.RuntimeException. So to create a custom UnCheckedException,we create a class that extends java.lang.RuntimeException:

In development time, developer was not required to handle RuntimeException by Editor or Java Compiler.

Result:

III. Sourcecode

CustomizeJavaException


Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*