Menu
Topics Index
...
`


Exceptions >
Siva Nookala - 20 Feb 2017
Java provides a way to define our own exceptions. It is good practice to defined the custom(user-defined) exceptions for every application. This helps in handling exceptions better and pass additional information along with exception. The hierarchy of custom exceptions can help taking appropriate actions at various module levels.

We can define custom exceptions by simply extending from the class Exception.
class CustomException extends Exception
{
    String moreInfo;
    
    CustomException(String moreInfo)
    {
        this.moreInfo = moreInfo;
    }
}
If we are dealing with railway reservation application, it might have custom exceptions like RailwayException, CounterClosedException, SeatsNotAvailableException, ServiceCancelledException, PaymentProcessingFailedException, InsufficientFundsException, PaymentGatewayNotRespondingException. And they could have a hierarchy as shown below. Railway-exceptions-hierarchy
The topic Java Throw Keyword - Java Throws Keyword explains how the user defined exceptions can be created and how they can be thrown.

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App