Today we will learn Exception Handling in Java, basically, The Exception handling is one of the powerful machines to switch the runtime mistakes. An Exception is an Event or an as Normal Condition is a program occurring during Execution of a Program that leads to disruption of the Normal flow of the Program Instructions.
The Process of Creating an Exception Object and Passing It to the Runtime System is tenured as throwing an Exception. An Appropriate Exception handler is one that handles the same type of Exception as the one thrown by the Method.
Checked Exceptions are Exception that a Well Written Application must anticipate and provide methods to the receiver from. Errors are Exceptions that are external to the Application, and the Application usually cannot anticipate or Receiver from Errors.
You May Also Like: Hospital Management System Project
Runtime Exception is Exceptions that are internal to the Application from which the Application usually cannot anticipate or Recover From. The throwable class is the base class of the Exception classes and has two direct Sub Classes and who have two direct subclasses Normally Exception and Error.
The Try Block is a Block of Code which might raise an Exception and Catch block is a Block of the code used to handle a particular type of Exception. The user can associate multiple Exception handles with a Try Block by providing more than one catch blocks directly after the try block.
The Exception Handling in Java Tutorial
Java provides the throw and throws keywords to Explicitly Raise an Exception in the Main () Method. Java Provides the finally block to Ensure Exception of Cleanup code Even when an Exception Occurs.
You May Also Like: Billing System Project in Java
The Final block is Executed Even if an Exception occurs in the try Block. The Throws clause is written after the method Name and Argument list and before the opening brace of the method. All methods use the throw statement to throw an Exception. The Throw statement takes a Single Argument which is a throwable Object.
Toe Create a Multiple Exception Catch Block, the Types of Exceptions that catch block can Hand are specified separated by a Vertical bar. The Try with resources statement is a try statement that decors one or more resources. A resource is an Object that must be chased after Program is finished.
To create a User-defined Exception Class, the class must inherit. Exception Wrapping is caught an Exception, Wrapping it in a different Exception, and throwing the Wrapper Exception. An Assertion is a statement in the Java that allows the programmer to test his/her Assumption about the Program. Assertions should not be used to check the Parameters of a Public Method.
I have shared below Exception Handling in java Codes, with Examples, you can get ideas about it.
Try Block Code:
// Array Show You ArrayIndexOutOfBoundsException
// User Input, If User Divide 50/0, You Can See Arithmetic Exception
System.out.println("Please Enter Number");
System.out.println(" Please Enter Number For Division");
System.out.println("The Division is:"+num/num1);
After That, You Can Here it Catch Block:
System.out.println(" Array Out of Arrange "+ar.getMessage());
That’s very simple script or code, you can understand as well, How you can use Exception Handling in Java. I don’t go too deep, Above I have shared Exception Handling in Java details. you can read compete and try to this code.
After that, you will understand as Well. If you have any question/suggestion, kindly comment me or email Me I will reply with you very soon.
Thanks for the read and visit my Blog/Website, kindly Subscribe My YouTube Channel and Follow on my Social network pages. Thanks Again.