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

  • SEARCH
  • Home
  • 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 9177511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:20:34+00:00 2026-06-17T17:20:34+00:00

It was always told me that Java exception handling is quite expensive. I’m asking

  • 0

It was always told me that Java exception handling is quite expensive.

I’m asking if is it a good practice creating an exception instance of a specific type at the beginning of the program and without creating a new one, throwing always the same exception object.

I just want to make an example. Common code:

if (!checkSomething(myObject))
   throw new CustomException("your object is invalid");

alternative:

static CustomException MYEXP = new CustomException("your object is invalid");

//somewhere else
if (!checkSomething(myObject))
    throw MYEXP;

Of course, I’m doing some assumptions here:

  1. MyCustomException has no parameters
  2. client code, whenever is a good practice or not, is heavlily based on exception handling and refactoring is not an option.

So questions are:

  1. Is this a good practice?
  2. Does this damage some JVM mechanism?
  3. If 1 is yes, there is the possibility of a performance gain? (I think not, but not sure)
  4. If 1 and 3 are yes, why is not sponsored as practice?
  5. If 1 is no, why Martin Odersky told in his introduction to Scala that this is how Scala works in some cases? (At minute 28.30 he tolds that break is implemented has throwing an exception, audience says that this is time consuming and he replies that exception is not created every time)Fosdem 2009

I hope this is not a idle/stupid question, I’m curious about this. I think that real cost in exception handling is handling and not creation.

edit
Added reference of precise discussion on FOSDEM presentation

disclaimer: none of my code works like proposed and I have no intention to manage exception like this, I’m just doing a “what-if” question and this curiosity is generated from the affermation that video. I thought: if it’s done in Scala, why is not in Java?

  • 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-17T17:20:35+00:00Added an answer on June 17, 2026 at 5:20 pm

    No, don’t do that. The expensive part is not handling the exception, it is generating the stacktrace. Unfortunately the stacktrace is also the useful part. If you throw a saved exception you will be passing on a misleading stacktrace.

    It could be that within the implementation of Scala there are situations where it makes sense to do this. (Maybe they are doing something recursive and want to generate an exception object upfront so in case they run out of memory they can still produce an exception.) They also have a lot of information about what they’re doing so they have a better chance of getting it right. But optimizations made by JVM language implementors are a very special case.

    So you wouldn’t be breaking anything, unless you think providing misleading information constitutes breakage. It seems like a big risk to me.

    Trying out Thomas Eding’s suggestion for how to create an exception with no stacktrace seems to work:

    groovy:000> class MyException extends Exception {
    groovy:001>     public Throwable fillInStackTrace() {}}
    ===> true
    groovy:000> e = new MyException()
    ===> MyException
    groovy:000> Arrays.asList(e.stackTrace)
    ===> []
    

    Also check out the JLS:

    The NullPointerException (which is a kind of RuntimeException) that is
    thrown by method blowUp is not caught by the try statement in main,
    because a NullPointerException is not assignable to a variable of type
    BlewIt. This causes the finally clause to execute, after which the
    thread executing main, which is the only thread of the test program,
    terminates because of an uncaught exception, which typically results
    in printing the exception name and a simple backtrace. However, a
    backtrace is not required by this specification.

    The problem with mandating a backtrace is that an exception can be
    created at one point in the program and thrown at a later one. It is
    prohibitively expensive to store a stack trace in an exception unless
    it is actually thrown (in which case the trace may be generated while
    unwinding the stack). Hence we do not mandate a back trace in every
    exception.

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

Sidebar

Related Questions

So I've always been told that it's bad practice to modify arguments to methods
I've been told recently a good practice in object oriented programming that you should
I have previously been told that I should always use Randomize() before I use
During the last years I always thought that in Java, Reflection is widely used
On one hand, I'm told that exceptions in C# are 'expensive', but on the
I used to always use tables. However, I've been told that the use of
I've always read and been told that when dealing with binary files that one
I recall that one should always avoid using recursive method calls in Java .
I've always been told that when debugging an application, JavaScript's console.log() method is preferred
I've been told that I should use size_t always when I want 32bit unsigned

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.