This tutorial aims to provide a step-by-step guide on creating custom exceptions in Java. Exceptions are events that disrupt the usual flow of the program. Java provides a robust exception handling mechanism with its built-in exception types, but in some scenarios, you may need to create your own custom exceptions, which is what we will be focusing on in this tutorial.
Upon completion of this tutorial, you will:
- Understand the importance and use-cases of custom exceptions.
- Be able to create your own custom exceptions in Java.
- Know how to use these custom exceptions in your code.
Prerequisites:
- Basic knowledge of Java programming language.
- Understanding of basic exception handling in Java.
In Java, all exceptions must be an instance of a class that derives from Throwable
. Usually, programs throw objects that extend the Exception
class. To create a custom exception, you have to define a new exception class. This class should extend from Exception
or one of its subclasses.
To create a custom exception, you follow these steps:
1. Create a new class. The name should end with "Exception" to make it clear that it's an exception class.
2. Make the class extend Exception
or one of its subclasses.
3. Define constructors in your exception class. Since exceptions are created with the throw
keyword, they need a constructor.
public class MyCustomException extends Exception {
public MyCustomException(String message) {
super(message);
}
}
In this code:
- We have defined a new exception class MyCustomException
that extends Exception
.
- The MyCustomException
class has a constructor that accepts a message of type String
.
- This message is passed to the superclass Exception
using the super
keyword.
Now this custom exception can be used in a program like this:
public class Main {
public static void main(String[] args) {
try {
throw new MyCustomException("This is a custom exception");
} catch (MyCustomException e) {
System.out.println(e.getMessage());
}
}
}
Output:
This is a custom exception
Custom exceptions can also have additional fields and methods. For example:
public class DetailedException extends Exception {
private int detailCode;
public DetailedException(String message, int detailCode) {
super(message);
this.detailCode = detailCode;
}
public int getDetailCode() {
return this.detailCode;
}
}
In this code:
- The DetailedException
class has an additional field detailCode
.
- The constructor takes two parameters: a String
message and an int
detail code.
- There is a getter method getDetailCode()
to retrieve the detail code.
This exception can be used like this:
public class Main {
public static void main(String[] args) {
try {
throw new DetailedException("Detailed exception", 1001);
} catch (DetailedException e) {
System.out.println(e.getMessage());
System.out.println("Detail code: " + e.getDetailCode());
}
}
}
Output:
Detailed exception
Detail code: 1001
In this tutorial, we have covered:
- The concept of custom exceptions in Java.
- How to create a simple custom exception.
- How to create a custom exception with additional fields.
Next steps for learning:
- Practice creating and using different types of custom exceptions.
- Learn more about the built-in exception classes in Java.
OutOfRange
that is thrown when a number is out of a specified range.OutOfRange
exception to include the range's lower and upper bounds as additional fields.Solutions:
public class OutOfRange extends Exception {
public OutOfRange(String message) {
super(message);
}
}
public class OutOfRange extends Exception {
private int lowerBound, upperBound;
public OutOfRange(String message, int lowerBound, int upperBound) {
super(message);
this.lowerBound = lowerBound;
this.upperBound = upperBound;
}
public int getLowerBound() {
return this.lowerBound;
}
public int getUpperBound() {
return this.upperBound;
}
}