In Java, we can create our own exceptions that are derived classes of the Exception class. Creating our own Exception is known as custom exception or user-defined exception. Basically, Java custom exceptions are used to customize the exception according to user need.
Consider the example 1
in which InvalidAgeException class extends the Exception class.
Using the custom
exception, we can have your own exception and message. Here, we have passed a
string to the constructor of superclass i.e. Exception class that can be
obtained using getMessage() method on the object we have created.
In this section, we
will learn how custom exceptions are implemented and used in Java programs.
62.5M
1K
Hello Java Program for Beginners
Why use custom exceptions?
Java exceptions cover
almost all the general type of exceptions that may occur in the programming.
However, we sometimes need to create custom exceptions.
Following are few of
the reasons to use custom exceptions:
- To catch and provide specific treatment
to a subset of existing Java exceptions.
- Business logic exceptions:
These are the exceptions related to business logic and workflow. It is
useful for the application users or the developers to understand the exact
problem.
In order to create
custom exception, we need to extend Exception class that belongs to java.lang
package.
Consider the following
example, where we create a custom exception named WrongFileNameException:
1.
public class WrongFileNameException extends Exception {
2.
public WrongFileNameException(String errorMessage) {
3.
super(errorMessage);
4.
}
5.
}
Note: We need to write the constructor that
takes the String as the error message and it is called parent class
constructor.
Example 1:
Let's see a simple
example of Java custom exception. In the following code, constructor of
InvalidAgeException takes a string as an argument. This string is passed to
constructor of parent class Exception using the super() method. Also the
constructor of Exception class can be called without using a parameter and
calling super() method is not mandatory.
TestCustomException1.java
1.
// class representing custom exception
2.
class InvalidAgeException extends Exception
3.
{
4.
public InvalidAgeException (String str)
5.
{
6.
// calling the constructor of parent Exception
7.
super(str);
8.
}
9.
}
10.
11.
// class that uses custom exception InvalidAgeException
12.
public class TestCustomException1
13.
{
14.
15.
// method to check the age
16.
static void validate (int age) throws InvalidAgeException{
17.
if(age < 18){
18.
19.
// throw an object of user defined exception
20.
throw new InvalidAgeException("age is not valid to vote");
21.
else {
22.
System.out.println("welcome to vote");
23.
}
24.
}
25.
26.
// main method
27.
public static void main(String args[])
28.
{
29.
try
30.
{
31.
// calling the method
32.
validate(13);
33.
}
34.
catch (InvalidAgeException ex)
35.
{
36.
System.out.println("Caught the exception");
37.
38.
// printing the message from InvalidAgeException object
39.
System.out.println("Exception occured: " + ex);
40.
}
41.
42.
System.out.println("rest of the code...");
43.
}
44.
}
Output:
Example 2:
TestCustomException2.java
1.
// class representing custom exception
2.
class MyCustomException extends Exception
3.
{
4.
5.
}
6.
7.
// class that uses custom exception MyCustomException
8.
public class TestCustomException2
9.
{
10.
// main method
11.
public static void main(String args[])
12.
{
13.
try
14.
{
15.
// throw an object of user defined exception
16.
throw new MyCustomException();
17.
}
18.
catch (MyCustomException ex)
19.
{
20.
System.out.println("Caught the exception");
21.
System.out.println(ex.getMessage());
22.
}
23.
24.
System.out.println("rest of the code...");
25.
}
26.
}
Output:
కామెంట్లు లేవు:
కామెంట్ను పోస్ట్ చేయండి