Over a million developers have joined DZone.

Performance Tip: Avoid Catching Exceptions

DZone's Guide to

Performance Tip: Avoid Catching Exceptions

Improve performance in your single thread app by catching the exceptions that can hinder performance and reduce response time.

· Performance Zone
Free Resource

Download our Introduction to API Performance Testing and learn why testing your API is just as important as testing your website, and how to start today.

In this post, we will look at simple example how catching exceptions impacts performance.

Code Sample

Code for Catching an Exception

private static void exceptionTest(){
 int i=0;
 int j=1;
 int k=j/i;
 }catch(ArithmeticException ex){
 //not good idea to catch run time exception but catch for demo only

Code Where Conditions are Handled

private static void withoutExceptionTest(){
 int i=0;
 int j=1;
 int k=j/i;

Method to Call Logic

private static long exceptionTestLoop(int iterations,boolean isCatchException){
 long startTime=System.nanoTime();
 for(int i=0;i<iterations;i++){
 long endTime=System.nanoTime();
 long time=endTime-startTime;
 return time;

Main Method

public static void main(String[] args) {
 int itr=1000000;
 long timeForCatching=exceptionTestLoop(itr,true);
 long time=exceptionTestLoop(itr,false);
 System.out.println("Time for catching Exception "+timeForCatching+" without catching "+time);




Avoid catching exceptions as it will reduce response time. Above, the example demonstrates results for a simple single threaded application; however, the situation will become worst in multi-threaded environment.

Find scaling and performance issues before your customers do with our Introduction to High-Capacity Load Testing guide.

java ,performance tuning ,exceptions

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}