Example 1: Java program to create custom checked exception
import java.util.ArrayList;
import java.util.Arrays;
// create a checked exception class
class CustomException extends Exception {
public CustomException(String message) {
// call the constructor of Exception class
super(message);
}
}
class Main {
ArrayList<String> languages = new ArrayList<>(Arrays.asList("Java", "Python", "JavaScript"));
// check the exception condition
public void checkLanguage(String language) throws CustomException {
// throw exception if language already present in ArrayList
if(languages.contains(language)) {
throw new CustomException(language + " already exists");
}
else {
// insert language to ArrayList
languages.add(language);
System.out.println(language + " is added to the ArrayList");
}
}
public static void main(String[] args) {
// create object of Main class
Main obj = new Main();
// exception is handled using try...catch
try {
obj.checkLanguage("Swift");
obj.checkLanguage("Java");
}
catch(CustomException e) {
System.out.println("[" + e + "] Exception Occured");
}
}
}
Output
Swift is added to the ArrayList [CustomException: Java already exists] Exception Occured
In the above example, we have extended the Exception
class to create a custom exception named CustomException. Here, we call the constructor of Exception
class from the CustomException class using super()
keyword.
Inside the method checkLanguage()
, we have checked the exception condition, and if the exception occurs, the try..catch block handles the exception.
Here, this is the checked exception. We can also create unchecked exception class in Java. To learn more on checked and unchecked exception, visit Java Exception.
Example 2: Create custom unchecked exception class
import java.util.ArrayList;
import java.util.Arrays;
// create a unchecked exception class
class CustomException extends RuntimeException {
public CustomException(String message) {
// call the constructor of RuntimeException
super(message);
}
}
class Main {
ArrayList<String> languages = new ArrayList<>(Arrays.asList("Java", "Python", "JavaScript"));
// check the exception condition
public void checkLanguage(String language) {
// throw exception if language already present in ArrayList
if(languages.contains(language)) {
throw new CustomException(language + " already exists");
}
else {
// insert language to ArrayList
languages.add(language);
System.out.println(language + " is added to the ArrayList");
}
}
public static void main(String[] args) {
// create object of Main class
Main obj = new Main();
// check if language already present
obj.checkLanguage("Swift");
obj.checkLanguage("Java");
}
}
Output
Swift is added to the ArrayList
Exception in thread "main" CustomException: Java already exists
at Main.checkLanguage(Main.java:21)
at Main.main(Main.java:37)
In the above example, we have extended the RuntimeException
class to create an unchecked custom exception class.
Here, you can notice that, we haven't declared any try...catch block. It is because the unchecked exception is checked at runtime.
Besides that, other functionality of unchecked exception is similar to the above mentioned program.