Custom Exception Classes java

Custom Exception Classes java

In Java programming, Java developers can create custom exceptions (also known as Java user-created or defined exceptions) to manage or handle particular condition scenarios in their software applications that are not covered by the Java built-in library exceptions. Remember, a custom exception is a new class created by a Java program that extends the constructor methods of either Exception (for checked exceptions) or RuntimeException (for unchecked exceptions) in an existing program.

Custom Exception Classes java

Rules for creating a custom exception in Java.

  • A custom exception class can be defined or created in any Java program by extending the Exception class or RuntimeException class as needed.
  • Provide constructors in existing Java programs that allow the Java program to pass a message or cause, similar to those in built-in exceptions.
  • In general, override function method concepts like toString() or printStackTrace() for custom class exception behavior.

Creating a Custom Checked Exception in Java.

A checked exception in Java is a program exception or error that must be declared or defined in the signature portion of a program method or handled in a function method defined in the program. All these steps extend the Exception class in the program.

Example of a Java Custom Checked Exception.

InsufficientCourseException(Checked Exception) explanation.

// Here we define a custom checked exception class.

public class InsufficientCourseException extends Exception {

// Here we define a class constructor that takes a custom error message.

public InsufficientCourseException(String message) {

super(message); // Here, pass the message to the superclass (Exception) class.

}

}

In the above example.

  • In this example, we create a custom checked exception, InsufficientCourseException, that extends the Exception class in the current program. We also provide a constructor that takes a custom error message as input.

Example of a custom checked exception in Java.

public class Student {

private double duration;

public Student(double duration) {

this.duration = duration;

}

// Here, we define a method to apply course that throws InsufficientCourseException.

public void selection(double amount) throws InsufficientCourseException {

if (amount > duration) {

throw new InsufficientCourseException(“Insufficient course for selection ” + amount);

} else {

duration -= amount;

System.out.println(“Course selection successful with new duration – ” + duration);

}

}

public static void main(String[] args) {

CourseSelection course = new CourseSelection(999);

try {

course.selection(1499); // here This will throw an InsufficientCourseException error

} catch (InsufficientCourseException e) {

System.out.println(“InsufficientCourseException caught here – ” + e.getMessage());

}

}

}

In the above example.

  • In this program, if the amount exceeds the duration, the selection() method automatically throws a custom exception, InsufficientCourseException.
  • User-defined custom exceptions are handled in the main() method by catching InsufficientCourseException and printing an error message.

Creating an unchecked custom exception in Java.

An unchecked exception in Java extends the RuntimeException concept. Unchecked exceptions defined in Java programs do not need to be declared or defined explicitly in a method signature. This type of program exception is commonly used to represent programming errors or unexpected program conditions that can be caught during a program’s runtime process.

Example of a StudentAgeException/UncheckedException in Java.

// Here we create a custom unchecked exception class

public class StudentAgeException extends RuntimeException {

// Here we define a constructor that takes a custom error message

public StudentAgeException(String message) {

super(message); // Here it passes the message to the superclass (RuntimeException) class

}

}

In this example.

  • Here we create a custom unchecked exception, StudentAgeException, which extends a RuntimeException in the existing program.

Example of a custom unchecked exception in Java.

public class Student {

private int age;

public Student(int age) {

if (age < 0 || age > 100) {

throw new StudentAgeException(“Invalid student age range ” + age + “. Please enter age between 0 and 100.”);

}

this.age = age;

}

public static void main(String[] args) {

try {

Student student = new Student(-10); // This will throw a StudentAgeException in the program.

} catch (StudentAgeException e) {

System.out.println(“Student age Exception caught here – ” + e.getMessage());

}

}

}

In this example.

  • the constructor of the Student class throws a StudentAgeException if the student’s age is negative or greater than 100.
  • Because StudentAgeException extends RuntimeException in the program, the exception does not need to be declared or caught explicitly, but it is still caught in the try-catch block.

Leave a Reply