When and How to use Try/Catch in Java example

When and How to use Try/Catch in Java example

Exception handling is really a good part should be taken care to provide the better understandable response to the user at all the time and scenarios. Handling exception in java can be done in many ways,

  • Throws
  • Try/Catch

Throws will just throw the exception to the previous layer, which will be used majorly in frameworks, since in frameworks only flow control will start from controller to DAO layers, so when some exception occurs in DAO/Service/Delegate layers error should be handled and thrown to the controller/previous layer to provide the corresponding response to the user.

Try/Catch: Particular block where you may get the exception should be put inside the try block, and catch will be provided to handle if any exception occurred in the try part.

Till now what we saw is basic and you would have already knowing very well. But now we are going to see how we are making use of the try/catch in our below program and what is the difference we are seeing when we are using try/catch in different different places in the same program.

Without Try/Catch:

[java]

package in.javadomain;

import java.util.ArrayList;

public class HowToUseTryCatchExample {
public static void main(String[] args){

ArrayList<String> myLst = new ArrayList<String>();

myLst.add("Naveen");
myLst.add(null);
myLst.add("www.ngdeveloper.com");
myLst.add("null");
myLst.add("Naveen kumar Gunasekaran");

for (String lstvalue : myLst) {

if (lstvalue.contains("www")) {
System.out.println("Yes this is site name ======>" + lstvalue);
} else {
System.out.println("No it is not a site name ======>" +lstvalue);
}

}

}
}
[/java]

Output:

[plain]
No it is not a site name ======>Naveen
Exception in thread "main" java.lang.NullPointerException
at in.javadomain.HowToUseTryCatchExample.main(HowToUseTryCatchExample.java:18)
[/plain]

Note: Full program is not executed, means when exception occurred, program stopped executing further.

With Try/Catch Case 1: [Program still terminates if exception occurs]

[java]

package in.javadomain;
import java.util.ArrayList;
public class HowToUseTryCatchExample {
public static void main(String[] args) {

ArrayList<String> myLst = new ArrayList<String>();

myLst.add("Naveen");
myLst.add(null);
myLst.add("www.ngdeveloper.com");
myLst.add("null");
myLst.add("Naveen kumar Gunasekaran");

try {
for (String lstvalue : myLst) {

if (lstvalue.contains("www")) {
System.out.println("Yes this is site name ======>" +lstvalue);
} else {
System.out.println("No it is not a site name ======>" +lstvalue);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
[/java]

Output:

[since we used arraylist, there may be some change in the order of the output]

[plain]
No it is not a site name ======>Naveen
java.lang.NullPointerException
at in.javadomain.HowToUseTryCatchExample.main(HowToUseTryCatchExample.java:19)
[/plain]

Even after try/catch implementation in our program, we could not see any difference, means program still terminates when exception occurs.

There may be some scenario where we want our program to execute the full program even after the exception,

Try/Catch with Case 2: [Program continue executing even after the exception]
[java]

package in.javadomain;
import java.util.ArrayList;
public class HowToUseTryCatchExample {
public static void main(String[] args) {
ArrayList<String> myLst = new ArrayList<String>();
myLst.add("Naveen");
myLst.add(null);
myLst.add("www.ngdeveloper.com");
myLst.add("null");
myLst.add("Naveen kumar Gunasekaran");
for (String lstvalue : myLst) {
try {
if (lstvalue.contains("www")) {
System.out.println("Yes this is site name ======>" +lstvalue);
} else {
System.out.println("No it is not a site name ======>" +lstvalue);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
[/java]

Output:

[plain]
java.lang.NullPointerException
at in.javadomain.HowToUseTryCatchExample.main(HowToUseTryCatchExample.java:18)
No it is not a site name ======>Naveen
Yes this is site name ======>www.ngdeveloper.com
No it is not a site name ======>null
No it is not a site name ======>Naveen kumar Gunasekaran
[/plain]

Explanation:
In the Case2 with Try/catch scenario, we have used try/catch inside the for loop so even after the exception it started continuing the flow for the next elements inside the for loop. But the Case 1 was having the try/catch outside the for loop so when exception occurred, it stopped and came out of the loop, so program got terminated without even processing all the elements of the for loop.
Hope you got clear idea on how to use try/catch in java programs.

Leave a Reply