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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:01:43+00:00 2026-05-27T07:01:43+00:00

I am having a piece of code and in the catch block I intentionally

  • 0

I am having a piece of code and in the catch block I intentionally add some piece of code as I am sure the flow will once come to catch.
So for a big project, is this type of coding leads to some more resource usage as logically if any exception occurs, lead to a new thread or hold in JVM so it means some resource utilization and I do have a way to avoid exception to occur and do the piece of code somewhere in proper place.

Let me clarify-

char ch = 'c'
try{
Integer.parse(character.toString(ch));
}(Exception e){
//SOME VERY IMPORTANT OPERATION LIKE LOGIC MATHEMATICAL BASED
}

Now the above piece of code will throw me NumberFormatException and inside the catch loop I added my piece of logic, Now the same thing I can avoid and write

char ch = 'c';
if(!Character.isDigit(ch))
 //SOME VERY IMPORTANT OPERATION LIKE LOGIC MATHEMATICAL BASED

Now I didn’t write any exception but it will work properly.

Now my question, is the 1st approach of catch coding will lead to some internal resource usage , I mean does JVM internally leads to some internal resource usage and this kind of catch coding should be avoided as max as possible or I can definately use the 1st approach

  • 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-05-27T07:01:44+00:00Added an answer on May 27, 2026 at 7:01 am

    It’s very hard to understand the question, but some general points:

    • Exceptions are for exceptional events. Exceptions should not be used for normal conditions. Throwing an exception is an expensive operation.

    • try/catch/finally blocks are not resource-expensive at all. JVMs are optimized to handle them very efficiently, because properly-written code uses them all over the place.

    The cost comes in when an exception occurs, not when you use try/catch/finally in your code. So you should feel free to use try/catch/finally all over the place, but avoid writing code that relies on exceptions in the normal course of things.

    For instance, here’s a bad example of using exceptions:

    /**
     * Gets a `Thingy` instance based on the given `Foo`.
     *
     * @param   foo     the `Foo`, or `null`
     * @return  the new `Thingy` based on the given `Foo`, or `null` if `foo`
     *          is `null`
     * @throws  ThingyException if no `Thingy` can be constructed for the given `Foo`
     */
    Thingy getThingyForFoo(Foo foo) {
    throws ThingyException
        ExpensiveThingyFactory factory = getExpensiveThingyFactory();
        try {
            return factory.getThingy(foo);
        }
        catch (NullPointerException npe) {
            return null;
        }
        finally {
            factory.release();
        }
    }
    

    This is bad because the method clearly says that passing in a null foo argument is a normal use case, but the code relies on factory.getThingy(foo) throwing a NullPointerException when you pass in foo = null. Since that’s a documented normal use-case, you code for it explicitly:

    /**
     * Gets a `Thingy` instance based on the given `Foo`.
     *
     * @param   foo     the `Foo`, or `null`
     * @return  the new `Thingy` based on the given `Foo`, or `null` if `foo`
     *          is `null`
     * @throws  ThingyException if no `Thingy` can be constructed for the given `Foo`
     */
    Thingy getThingyForFoo(Foo foo) {
    throws ThingyException
        ExpensiveThingyFactory factory = null;
        if (foo == null) {
            return null;
        }
        factory = getExpensiveThingyFactory();
        try {
            return factory.getThingy(foo);
        }
        finally {
            factory.release();
        }
    }
    

    The try/finally in that is not expensive. Throwing an exception is expensive.

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

Sidebar

Related Questions

I'm having some problems with this piece of code. I've included a class declaration
I am having some trouble with a piece of code below: Input: li is
I am having trouble with the most simple piece of code. For some reason,
I'm having some problems with a certain piece of code which sits in my
Guys im having some trouble with some inconsistency of running a piece of code
I'm having some trouble with a particular piece of code, if anyone can enlighten
I'm having some trouble understanding this piece of code that my professor used as
I am having problem getting this piece of code to run. The class is
I am having some problems parsing this piece of XML using SimpleXML. There is
I'm having some problems with a piece of python work. I have to write

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.