Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8065207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:35:37+00:00 2026-06-05T11:35:37+00:00

I know that going into a catch block has some significance cost when executing

  • 0

I know that going into a catch block has some significance cost when executing a program, however, I was wondering if entering a try{} block also had any impact so I started looking for an answer in google with many opinions, but no benchmarking at all. Some answers I found were:

  1. Java try/catch performance, is it recommended to keep what is inside the try clause to a minimum?
  2. Try Catch Performance Java
  3. Java try catch blocks

However they didn’t answer my question with facts, so I decided to try it for myself.

Here’s what I did. I have a csv file with this format:

host;ip;number;date;status;email;uid;name;lastname;promo_code;

where everything after status is optional and will not even have the corresponding ; , so when parsing a validation has to be done to see if the value is there, here’s where the try/catch issue came to my mind.

The current code that I inherited in my company does this:

StringTokenizer st=new StringTokenizer(line,";");  
String host = st.nextToken();
String ip = st.nextToken();
String number = st.nextToken();
String date = st.nextToken();
String status = st.nextToken();                             
String email = "";
try{
    email = st.nextToken();
}catch(NoSuchElementException e){
    email = "";
}

and it repeats what it’s done for email with uid, name, lastname and promo_code.

and I changed everything to:

if(st.hasMoreTokens()){
    email = st.nextToken();
}

and in fact it performs faster. When parsing a file that doesn’t have the optional columns. Here are the average times:

 --- Trying:122 milliseconds
 --- Checking:33 milliseconds

however, here’s what confused me and the reason I’m asking: When running the example with values for the optional columns in all 8000 lines of the CSV, the if() version still performs better than the try/catch version, so my question is

Does really the try block does not have any performance impact on my code?

The average times for this example are:

--- Trying:105 milliseconds
--- Checking:43 milliseconds

Can somebody explain what’s going on here?

Thanks a lot

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T11:35:38+00:00Added an answer on June 5, 2026 at 11:35 am

    Yes, try (in Java) does not have any performance impact. The compiler generates no VM statements for a try block. It simply records the program counters between which the try block is active and attaches this information to the method in the class file. Then, when an exception is thrown, the VM unwinds the stack and checks at each frame whether the program counter in that frame is in a relevant try block. This (together with building the stack trace) is quite costly, so catching is expensive. However, trying is free :).

    Still, it is not good practice to use exceptions for regular control flow.

    The reason why your code performs faster is probably that catching is so extremely costly that it outweights the time saved by replacing the check by a simple try.

    Try catch can be faster in code where the catch is triggered not very often, e.g., if you go into the try 10000 times but only catch once, the try method would be faster than the if-check. Still, this is no good style and your way of explicitly checking for more tokens is to be preferred.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that the MySQL errors are split into client and server types. Has
If you'd like to develop a WebApp which you know that is going to
I know that there are more practical ways of going about what I'm trying
Need to know this so that i could send DTMF and that is going
Hi guys i am going to make a android game. i wanna know that
I know this is going to be something simple that I'm just missing somehow,
I have to stop windows from going into sleep when my program is running.
I've written some code that retrives a zip file and unzips it into a
Problem statement I have a worker thread that basically scans a folder, going into
I know that it's a subject that can raise a lot of debate, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.