try block
try block in java is used to enclose the code that might throw an exception. It must be used within the method.
If an exception occurs at the particular statement in the try block, the rest of the block code will not execute. So, it is recommended not to keep the code in try block that will not throw an exception.
try block in java must be followed by either catch or finally block.
Syntax of Java try-catch
try{//code that may throw an exception}catch(Exception_class_Name ref){}
Syntax of try-finally block
try{//code that may throw an exception}finally{}
catch block
the catch block is used to handle the Exception by declaring the type of exception within the parameter. The declared exception must be the parent class exception ( i.e., Exception) or the generated exception type. However, the good approach is to declare the generated type of exception.
The catch block must be used after the try block only. You can use multiple catch blocks with a single try block.
The JVM firstly checks whether the exception is handled or not. If an exception is not handled, JVM provides a default exception handler that performs the following tasks:
- Prints out exception description.
- Prints the stack trace (Hierarchy of methods where the exception occurred).
- Causes the program to terminate.
But if the application programmer handles the exception, the normal flow of the application is maintained, i.e., the rest of the code is executed.
What is the problem without exception handling
Let's try to understand the problem if we don't use a try-catch block.
Example 1
Example1.java
public class Example1 {
public static void main(String[] args) {
int value = 100/0; //may throw exception
System.out.println("the rest of the code");
}
}
Output:-
java.lang.ArithmeticException: / by zero
As displayed in the above example, the rest of the code is not executed (in such a case, the rest of the code statement is not printed).
There might be 100 lines of code after the exception. If the exception is not handled, all the code below the exception won't be executed.
Solution by exception handling
Let's see the solution to the above problem by a java try-catch block
Example 2
Example2.java
public class Example2 {
public static void main(String[] args) {
try
{
int value = 100/0; //may throw exception
}
catch(ArithmeticException e) //handling the exception
{
System.out.println(e);
}
System.out.println("the rest of the code");
}
}
Output:-
java.lang.ArithmeticException: / by zero
the rest of the code
Example 3
Example3.java
public class Example3 {
public static void main(String[] args) {
try
{
int data=50/0; //may throw exception
// if exception occurs, the remaining statement will not exceute
System.out.println("rest of the code");
}
// handling the exception
catch(ArithmeticException e)
{
System.out.println(e);
}
}
}
Output:-
java.lang.ArithmeticException: / by zero
Here, we can see that if an exception occurs in the try block, the rest of the block code will not execute.
Example 4
Example4.java
public class Example4 {
public static void main(String[] args) {
try
{
int data=50/0; //may throw exception
}
// handling the exception by using Exception class
catch(Exception e)
{
System.out.println(e);
}
System.out.println("the rest of the code");
}
}
Output:-
java.lang.ArithmeticException: / by zero
the rest of the code
Example 5
Example5.java
public class Example5 {
public static void main(String[] args) {
try
{
int data = 100/0; //may throw exception
}
// handling the exception
catch(Exception e)
{
// displaying the custom message
System.out.println("The value can't divided by zero");
}
}
}
Output:-
the value can't devided by zero
Example 6
Example6.java
public class Example6 {
public static void main(String[] args) {
int i=100;
int j=0;
int data;
try
{
data=i/j; //may throw exception
}
// handling the exception
catch(Exception e)
{
// resolving the exception in catch block
System.out.println(i/(j+2));
}
}
}
Output:-
50
Example 7
Example7.java
public class Example7 {
public static void main(String[] args) {
try
{
int data1=50/0; //may throw exception
}
// handling the exception
catch(Exception e)
{
// generating the exception in catch block
int data2=50/0; //may throw exception
}
System.out.println("rest of the code");
}
}
Output:-
java.lang.ArithmeticException: / by zero
Example 8
In the below example, we handle the generated exception (Arithmetic Exception) with a different type of exception class (ArrayIndexOutOfBoundsException)
Example8.java
public class Example8 {
public static void main(String[] args) {
try
{
int data=50/0; //may throw exception
}
// try to handle the ArithmeticException using ArrayIndexOutOfBoundsException
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}
System.out.println("therest of the code");
}
}
Output:-
java.lang.ArithmeticException: / by zero
0 Comments