Java try-catch block
Syntax of Java try-catch
[Link]{
2.//code that may throw an exception
3.}catch(Exception_class_Name ref){}
Syntax of try-finally block
[Link]{
2.//code that may throw an exception
3.}finally{}
Problem without exception handling
public class TryCatchExample1 {
public static void main(String[] args) {
int data=50/0; //may throw exception
[Link]("rest of the code");
}
Output:
Exception in thread "main"
[Link]: / by zero
Solution by exception handling
public class TryCatchExample2 {
public static void main(String[] args) {
try
{
int data=50/0; //may throw exception
}
//handling the exception
catch(ArithmeticException e)
{
[Link](e);
}
[Link]("rest of the code");
}
}
Output:
[Link]: / by zero
rest of the code
Example 3
In this example, we also kept the code in a try
block that will not throw an exception.
public class TryCatchExample3 {
public static void main(String[] args) {
try
{
int data=50/0; //may throw exception
// if exception occurs, the
remaining statement will not exceute
[Link]("rest of the code");
}
// handling the exception
catch(ArithmeticException e)
{
[Link](e);
}
}
}
Output:
[Link]: / 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
Here, we handle the exception using the parent
class exception.
[Link]
public class TryCatchExample4 {
public static void main(String[] args) {
try
{
int data=50/0; //may throw exception
}
// handling the exception by using
Exception class
catch(Exception e)
{
[Link](e);
}
[Link]("rest of the code");
}
}
Output:
[Link]: / by zero
rest of the code
Example 5
Let's see an example to print a custom message
on exception.
[Link]
public class TryCatchExample5 {
public static void main(String[] args) {
try
{
int data=50/0; //may throw exception
}
// handling the exception
catch(Exception e)
{
// displaying the custom message
[Link]("Can't divided by zero");
}
}
Output:
Can't divided by zero
Example 6
Let's see an example to resolve the exception in a
catch block.
[Link]
public class TryCatchExample6 {
public static void main(String[] args) {
int i=50;
int j=0;
int data;
try
{
data=i/j; //may throw exception
}
// handling the exception
catch(Exception e)
{
// resolving the exception in catch block
[Link](i/(j+2));
}
}
}
Output:
25
Example 7
In this example, along with try block, we also
enclose exception code in a catch block.
[Link]
public class TryCatchExample7 {
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
}
[Link]("rest of the code");
}
}
Output:
Exception in thread "main"
[Link]: / by zero
Here, we can see that the catch block didn't
contain the exception code. So, enclose exception
code within a try block and use catch block only to
handle the exceptions.
Example 8
In this example, we handle the generated
exception (Arithmetic Exception) with a different
type of exception class
(ArrayIndexOutOfBoundsException).
[Link]
public class TryCatchExample8 {
public static void main(String[] args) {
try
{
int data=50/0; //may throw exception
}
// try to handle the ArithmeticException
using ArrayIndexOutOfBoundsException
catch(ArrayIndexOutOfBoundsException e)
{
[Link](e);
}
[Link]("rest of the code");
}
}
Output:
Exception in thread "main"
[Link]: / by zero
Example 9
Let's see an example to handle another
unchecked exception.
[Link]
public class TryCatchExample9 {
public static void main(String[] args) {
try
{
int arr[]= {1,3,5,7};
[Link](arr[10]); //may throw
exception
}
// handling the array exception
catch(ArrayIndexOutOfBoundsException e)
{
[Link](e);
}
[Link]("rest of the code");
}
}
Test it Now
Output:
[Link]: 10
rest of the code
Example 10
Let's see an example to handle checked
exception.
[Link]
import [Link];
import [Link];
public class TryCatchExample10 {
public static void main(String[] args) {
PrintWriter pw;
try {
pw = new PrintWriter("[Link]"); //may throw
exception
[Link]("saved");
}
// providing the checked exception handler
catch (FileNotFoundException e) {
[Link](e);
}
[Link]("File saved successfully");
}
}
Test it Now
Output:
File saved successfully