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 7794189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:42:37+00:00 2026-06-01T22:42:37+00:00

The question says it all. Although the hit is not highly significant (I measured

  • 0

The question says it all. Although the hit is not highly significant (I measured it to be between 1.5x to 2x slower), there’s no difference between the byte code with try-catch and the byte code without it. So what makes it generally slower?

Pl. note that the question is not about overhead of throwing an exception, but of entering/leaving a try block.

EDIT:
here’s the code (run on Hotspot 1.6.0_31 server)

static void tryCatch()
{
    int i = 0;
    long l1 = getTime();
    for(int j = 0; j < 100000; j++)
    {
        try
        {
            i++;                
        }
        catch(Exception e)
        {

        }
    }
    long l2 = getTime();
    System.out.println("with try-catch: " + (l2 - l1) + ": " + i);      
}

static void noTryCatch()
{
    int i = 0;
    long l1 = getTime();
    for(int j = 0; j < 100000; j++)
    {
        i++;
    }
    long l2 = getTime();
    System.out.println("w/o  try-catch: " + (l2 - l1) + ": " + i);
}

static long getTime()
{
    return System.nanoTime();       
}
  • 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-01T22:42:39+00:00Added an answer on June 1, 2026 at 10:42 pm

    Since you have a micro-benchmark its is more likely you are testing how confusing the try/catch block is to the JVM compiler. For example, the JVM can be smart enough to change

    for(int j = 0; j < 100000; j++) {
        i++;
    }
    

    into

    i += 100000 * 1;
    

    using the try/catch block may prevent the more aggresive optimisations, but might not make any difference for a more realistic block of code.


    In any case I would normally change something like

    for(int j = 0; j < 100000; j++) {
        try {
            // do something
        } catch(Exception e) {
            // break or return
        }
    }
    

    .

    try {
        for(int j = 0; j < 100000; j++) {
            // do something
        }
    } catch(Exception e) {
        // continue or return
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Question says it all. I tried browsing through the latest tag but could not
Question says it all. Some quick code examples of usage would be nice.. thanks!
Question says it all, really. I'm not sure what the problem is. I'm fairly
Question says it all ... is there a way to generate more than 50
Question says it all. Is there a way to enforce case sensitivity in VB.Net
Question says it all. Is there a limit of files a folder can have?
The question says it all. Can I compile C# to native code and boot
Question pretty much says it all. Is there an equivalent for org.hibernate.Version.getVersionString() in EclipseLink?
Question says it all. Is there a way I can see what version of
The question says it all. To clarify: I am not trying to hide the

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.