Throwing Exceptions java

Throwing Exceptions in java

In Java programming, the throw keyword is used to throw a program exception. Java users can throw a custom program exception to indicate that a program error or exceptional condition has occurred within a user-defined function method or due to a specific event trigger within the current program. By throwing a custom Java exception, Java users can stop the normal execution process or flow of a program and transfer the current program’s default flow control to a properly matched catch block, where multiple user-defined program exception conditions can be handled.

Throwing Exceptions java

Throwing Exceptions with the Java throw Keyword.

The throw keyword in a Java program is used to throw custom exceptions from user-defined program source code in a clear order. Java users can define their own exceptions, including built-in exceptions in the Java library, such as NullPointerException or IOException, and custom, user-defined exceptions, according to their programming needs. They can throw both of these exceptions in their programs if needed.

Syntax for Throwing Exceptions.

throw new ExceptionType(“Custom Exception Error Message”);

Here in Throwing Exceptions.

  • The ExceptionType in the Java program is the type or nature of the user-defined Java exception that the Java user is throwing in the current program. It can be any user-defined class that extends the Throwable class if needed.
  • Similarly, “Error Message” is an optional user-defined custom exception message that indicates a user-defined custom exception error information to the program.

Throwing Built-in Exceptions in Java.

Java users can throw custom program exceptions, such as NullPointerException or ArithmeticException, using the throw keyword, or any built-in exception message condition in the current program.

Example of throwing an ArithmeticException in a Java program.

public class ThrowExcceptionIllustration {

public static void main(String[] args) {

int p = 10;

int q = 0;

try {

if (q == 0) {

throw new ArithmeticException(“Here p & q variable divide zero is not permitted”);

} else {

int output = p / q;

}

} catch (ArithmeticException e) {

System.out.println(“Here user-defined custom Exception caught – ” + e.getMessage());

}

}

}

In this example.

  • In this program, when q is 0, it throws an ArithmeticException explicitly to prevent division by zero. The user-defined custom exception is caught in the program’s created catch block, and a valid message is displayed as a result.

Throwing a Custom Exception in a Java Program.

Java users can throw a custom exception (a user-defined manual exception) by applying the throw keyword in a Java program, just like a built-in library program exception.

An example of throwing a custom exception in Java is InsufficientCourseException.

public class InsufficientCourseException extends Exception {

public InsufficientCourseException(String message) {

super(message);

}

}

public class Student {

private double duration;

public Student(double duration) {

this.duration = duration;

}

public void Selection(double amount) throws InsufficientCourseException {

if (amount > duration) {

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

} else {

duration -= amount;

System.out.println(“The selected course duration – ” + duration);

}

}

public static void main(String[] args) {

Student data = new Student(999);

try {

data.selection(10999); // here This will throw an InsufficientCourseException error

} catch (InsufficientCourseException e) {

System.out.println(“user define program exception caught – ” + e.getMessage());

}

}

}

Here in this example.

  • Here in this program, the Selection() function method checks whether the selected course amount is greater than the available course duration or not.
  • If it is, it throws an InsufficientCourseException and displays a custom error message information.
  • Similarly, the exception is defined in the catch block, where it displays a user-defined custom info message.

Leave a Reply