Exceptions are runtime anomalies or unusual conditions that a program may encounter during executing. Anomalies might include conditions such as division by zero, access to an array outside of is bounds, or running out of memory or disk space.
Exceptions are of two kinds, namely, synchronous exceptions and asynchronous exceptions. Errors such as “out of range index” and “overflow” belong to the synchronous type exceptions. The errors that are caused by events beyond the control of the program are called asynchronous exceptions.
The purpose of exception handling is to provide a way to detect and report an “exceptional circumstance” so that the appropriate action can be taken.
Java Exception Handling Keywords
There are 5 keywords used in Java exception handling.
- Try
- Catch
- Finally
- Throw
- Throws
Advantage of Exception Handling
The core advantage of exception handling is to maintain the normal flow of the application. Exceptions normally disrupt the normal flow of the application. That is why we use exception handling. Let's take a scenario:
Assume there are 3 statements in your program and there occurs an exception in the first at statement, the rest of the code will not be executed. In other words the second and third statements will not run. If we perform exception handling then the rest of the exception will be executed. That is why we use exception handling in Java.
Example
In this program we use try and catch.
- class demo
- {
- public static void main(String arg[])
- {
- try
- {
- System.out.println(5/0);
- }
- catch(Exception e)
- {
- System.out.println("U can't devide by zero");
- }
- System.out.println("Hello india");
- }
- }
Step 1
Name it "excep.java"and save the file in any location. I saved it at "c:/kiran/program".
Step 2
Set the path, without the path setting our Java file is not compiled.
Step 3
Now use the following command for checking compiling the Java file.
javac excep.java.
My Java program compiled successfully.
Step 4
Write the following code in the command prompt. Press Enter and see the output.
Java demo
Step 5
If we use (5/3) then the output is:
Step 6
Now, use the try, catch and finally.
Code
- class demo
- {
- public static void main(String arg[])
- {
-
- try
- {
- System.out.println(5/4);
- }
- catch(Exception e)
- {
- System.out.println("U Can't Divided by Zero");
- }
- finally
- {
- System.out.println("Hello india");
- }
-
- }
- }
Output
Step 7
If we use (5/0) the output is:
In other words, the finally block always executed whether an exception occurs or not.
Step 8
Now, we use try, catch and throw.
Code
- class MyExcep extends Exception
- {
- String msg()
- {
- return "U are not valid";
- }
- }
- class CoreExcep
- {
- void Checkage(int age) throws Exception
- {
- if(age<=18)
- {
- throw new MyExcep();
- }
- else
- {
- System.out.println("U are valid");
- }
- }
- public static void main(String arg[]) throws Exception
- {
- CoreExcep ce=new CoreExcep();
- try{
- ce.Checkage(20);
- }
- catch (MyExcep e){
- System.out.println(e.msg());}
- }
- }
Step 9
If we use 17 instead of 20.
Code
- class MyExcep extends Exception
- {
- String msg()
- {
- return "U are not valid";
- }
- }
- class CoreExcep
- {
- void Checkage(int age) throws Exception
- {
- if(age<=18)
- {
- throw new MyExcep();
- }
- else
- {
- System.out.println("U are valid");
- }
- }
- public static void main(String arg[]) throws Exception
- {
- CoreExcep ce=new CoreExcep();
- try{
- ce.Checkage(17);
- }
- catch (MyExcep e){
- System.out.println(e.msg());}
- }
- }
See the output