English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java flow control

Java Array

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List

Java Queue (queue)

Java Map collection

Java Set collection

Java Input/Output (I/O)

Java Reader/Writer

Java other topics

Java program creates custom exceptions

Java Examples Comprehensive

In this example, we will learn how to create custom checked and unchecked exceptions in Java.

To understand this example, you should understand the followingJava programmingTopic:

Example1: Create a Java program for custom check exceptions

import java.util.ArrayList;
import java.util.Arrays;
//Create a check exception class
class CustomException extends Exception {
  public CustomException(String message) {
    //Call the constructor of the Exception class
    super(message);
  }
}
class Main {
  ArrayList<String> languages = new ArrayList<>(Arrays.asList("Java", "Python", "JavaScript"));
  //Check exception
  public void checkLanguage(String language) throws CustomException {
    // If language already exists in ArrayList, then throw an exception
    if(languages.contains(language)) {
      throw new CustomException(language + " already exists
    }
    else {
      // Insert language into ArrayList
      languages.add(language);
      System.out.println(language + " added to ArrayList
    }
  }
  public static void main(String[] args) {
    // Create an object of the Main class
    Main obj = new Main();
    //Use try...catch to handle exceptions
    try {
      obj.checkLanguage("Swift");
      obj.checkLanguage("Java");
    }
    catch(CustomException e) {
      System.out.println("[") + e + "] Exception occurred
    }
  }
}

Output Result

Swift added to ArrayList
[CustomException: Java already exists] Exception occurred

In the above example, we extended the Exception class to create a custom exception named CustomException. Here, we use the super() keyword to call the constructor of the Exception class from the CustomException class.

In the method checkLanguage(), we checked for exceptions, and if an exception occurred, the try..catch block will handle the exception.

Here, this is a checked exception. We can also create an unchecked exception class in Java. For more information on checked and unchecked exceptions, please visitJava Exception.

Example2: Create a custom unchecked exception class

import java.util.ArrayList;
import java.util.Arrays;
//Create an 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 for exceptions
  public void checkLanguage(String language) {
    //If the programming language already exists in the ArrayList, an exception is thrown
    if(languages.contains(language)) {
      throw new CustomException(language + " already exists
    }
    else {
      //Insert programming language into ArrayList
      languages.add(language);
      System.out.println(language + " added to ArrayList
    }
  }
  public static void main(String[] args) {
    //Create an object of the Main class
    Main obj = new Main();
    //Check if the programming language already exists
    obj.checkLanguage("Swift");
    obj.checkLanguage("Java");
  }
}

Output Result

Swift is being added to 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 extended the RuntimeException class to create unchecked custom exception classes.

Here, you may have noticed that we have not declared anytry ... catchBlock. This is because unchecked exceptions are checked at runtime.

Other functionalities not checked for exceptions are similar to the above program.

Java Examples Comprehensive