EXCEPTION HANDLING IN JAVA
Hey guys, we are back with another blog of java and today we
are gonna talk about exception handling in java. Exception handling refers to pre-handling
the runtime errors in the code and predefining the action to implement if that
exception occurs in the code. Java provide robust and object oriented way to
provide handle exception scenarios known as java exception handling. Let’s get
into examples to understand it better.
Ex:
public class Coding_Winds {
public static void main(String args[]) {
int i, j=2, k=0;
i=j/k;
System.out.println(i);
}
}
For the above code , if we will run it we will get a runtime
error and we will get the following message on the console
Exception in thread "main" java.lang.ArithmeticException: / by zero
at solution.Student.main(Coding_Winds.java:10)
Now if we will handle this following exception in the code,
then we can deal with it in an specified way. So for the above case if we use the exception handling. Below
is the code after the implementation of exception handling.
public class Coding_Winds {
public static void main(String args[]) {
try {
int i, j=2, k=0;
i=j/k;
} catch(ArithmeticException e) {
System.out.println(e.getMessage()+" occurred");
}
}
}
Now to implement exception handling, we enclose the code that
is likely to through an exception within curly braces and before opening curly
braces we use the try keyword and the catch keyword after the
ending braces. And inside the parenthesis after
the catch keyword we can put the type of error that we likely to get. In
the above case we are using an arithmetic operator so we are likely to get an
arithmetic error. And after mentioning the type of error we have to mention an
object ‘e’ , and we can use this to call few methods we have. And after this we can again put the set of code
within the curly braces, in which we can call some methods, if any error occurs
during runtime. For ex in this case the method ‘.getMessage()’ returns the
problem which is causing the error.
For the above code , we will receive the following message on
the console
/
by zero occurred
Now if you want to execute the code, below the code which you
think may throw error at runtime, then you can execute it.
For ex:
public static void main(String args[]) {
try {
int i, j=2, k=0;
i=j/k;
} catch(ArithmeticException e) {
System.out.println(e.getMessage()+" occurred");
}
System.out.println("Some important code");
}
}
Now the output on the
console for the following code is
/ by zero occurred
Some important code
And if you wouldn’t have used the exception handling code for
the above code then the compiler wouldn’t have reached down to the part , after
the code which throws exception.
For example in the below code ,the line saying “Some
important code” will not get printed.
public class Coding_Winds {
public static void main(String args[]) {
int i, j=2, k=0;
i=j/k;
System.out.println("Some important code");
}
}
Now on running this code, there will be only the following
message on the compiler
Exception in thread "main" java.lang.ArithmeticException: / by zero
at
solution.Student.main(Coding_Winds.java:9)
Do let us know your reviews in the comment section below and
we will be discussing more on exception handling in the blogs that will be coming
up in future.