The Java keywords for managing exceptions include throw, throws, and finally.
To throw an exception explicitly in Java, use the throw keyword.
The exception object that is to be thrown is specified. The notice that comes with the exception contains a description of the error. These anomalies could be caused by user inputs, the server, etc.
Java’s throw keyword allows us to throw either verified or unchecked exceptions. It mostly serves as a custom exception thrower.
throw new exception_class("error message");
public class Demo {
//function to check if a student passed or failed
public static void validate(int percent) {
if(percent<30) {
throw new ArithmeticException("Student Failed");
}
else {
System.out.println("Student Passed!!");
}
}
//main method
public static void main(String args[]){
//calling the function
validate(33);
System.out.println("\n---------");
}
}
When percent is 33%.
Student Passed!!
---------
When percent is 28%. The exception is successfully handled.
Exception in thread "main" java.lang.ArithmeticException: Student Failed
at Demo.validate(Demo.java:6)
at Demo.main(Demo.java:15)
Declaring an exception in Java requires the throws keyword. The programmer receives a notification that an exception might happen. Therefore, the programmer should provide the exception handling code to retain the program’s usual flow.
The checked exceptions are often handled through exception handling. It is the responsibility of the programmer to check the code before using it if an unchecked exception like NullPointerException occurs.
return_type method_name() throws exception_class_name{
//method code
}
// Java program to demonstrate working of throws
class Demo {
static void fun() throws IllegalAccessException
{
System.out.println("Inside fun(). ");
throw new IllegalAccessException("demo");
}
// This is a caller function
public static void main(String args[])
{
try {
fun();
}
catch (IllegalAccessException e) {
System.out.println("caught in main.");
}
}
}
Inside fun().
caught in main.
The finally block in Java is used to run crucial code, such as connection closure, among other things.
Whether an exception is handled or not, the Java finally block is always run. Therefore, regardless of whether an exception happens or not, it contains all the relevant statements that must be printed.
The try-catch block is followed by the finally block.
// Java program to demonstrate working of try,
// catch and finally
class Demo {
public static void main(String[] args)
{
int a = 10, b = 5, c = 5, result;
try {
result = a / (b - c);
System.out.println("result" + result);
}
catch (ArithmeticException e) {
System.out.println("Exception caught:Division by zero");
}
finally {
System.out.println("I am in final block");
}
}
}
Exception caught:Division by zero
I am in final block
Note: also read about the Nested try block & try with Resource Statement
If you like my post, please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…