Write a Java program to throw IllegalAccessException, Handle the Exception using try and catch block – Java Tutorial
Problem Definition:
Write a program that contains one method which will throw IllegalAccessException and use proper exception handlers so that exception should be printed.
Solution:
The exception is an unusual situation in a program that may lead to a crash the program. Usually, it indicates the error.
Video tutorial – Java program to throw llegalAccessException
Various keywords used in handling the exception are –
try – A block of source code that is to be monitored for the exception.
catch – The catch block handles the specific type of exception along with the try block. Note that for each corresponding try block there exists the catch block.
finally – It specifies the code that must be executed even though an exception may or may not occur.
throw – This keyword is used to throw a specific exception from the program code.
throws – It specifies the exceptions that can be thrown by a particular method.
The Java code that you may think may produce an exception is placed within the try block and the exception should be handled in the associated catch block.
Java program to throw IllegalAccessException, Handle the Exception using try and catch block
class Test { static void fun() throws IllegalAccessException { System.out.println("Inside the function"); throw new IllegalAccessException("Illegal Exception"); } public static void main(String args[]) { try { fun(); } catch(IllegalAccessException e) { System.out.println(e); } } }
Output:
Inside the function java.lang.IllegalAccessException:
Illegal Exception
Summary:
In this article, we understood, how to write a Java program to throw IllegalAccessException, and Handle the Exception using try and catch block – Java Tutorial. If you like the tutorial share it with your friends. Like the Facebook page for regular updates and the YouTube channel for video tutorials.