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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:32:23+00:00 2026-05-26T06:32:23+00:00

This is a general programming question, not pertaining to any specific language. A new

  • 0

This is a general programming question, not pertaining to any specific language.

A new programmer typically will write a method that calculates some value, then returns the value:

public Integer doSomething()
{
   // Calculate something
   return something;
}

public void main()
{
  Integer myValue = doSomething();
}

But when an exception occurs during the calculation of something, what is the best way to handle the exception, especially when giving the user feedback? If you do a try/catch of the calculation of something and if an exception is caught, what do you return? Nothing was calculated, so do you return null? And once you return it (whatever it may be), do you need to do another try/catch in the parent method that checks to see if a valid value was returned? And if not, then make sure the user is given some feedback?

I have heard arguments on both sides of the table about never returning values at all, but instead setting calculated values as pointers or global variables and instead returning only error codes from methods, and then (in the parent method) simply handling the error codes accordingly.

Is there a best practice or approach to this? Are there any good resources that one could access to learn more about the best way to handle this?

UPDATE for Clarification

Consider the following code:

public void main()
{
  int myValue = getMyValue();

  MyUIObject whatever = new MyUIObject();
  whatever.displayValue(myValue); // Display the value in the UI or something
}

public Integer getMyValue()
{
  try
  {
    // Calculate some value
  } catch (exception e) {
    // ??
  }
  return value;
}

I call the method to get some int value, then I return it. Back in main(), I do something with the value, like show it in the Log in this case. Usually I would display the value in the UI for the user.

Anyways, if an exception is caught in getMyValue(), so does value get returned but it’s null? What happens in main() then? Do I have to test if it’s a valid value in main() as well?

I need the program to handle the error accordingly and continue. Someone below suggested displaying the appropriate information in the UI from within the getMyValue() method. I see two potential issues:

  1. It seems like I would be mixing the business logic with (in this case) the logic for the UI.
  2. I would have to pass a reference of the MyUIObject to the getMyValue() or something so I could access it from within the function. In the above simple example that is no big deal, but if there is a BUNCH of UI elements that need to be updated or changed based off of what happens in getMyValue(), passing them all might be a bit much…

I’ve read a bunch about the fundamentals of all of this but I can’t seem to find a straight answer for the above situation. I appreciate any help or insight.

  • 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-26T06:32:24+00:00Added an answer on May 26, 2026 at 6:32 am

    I think you do not quite understand exceptions.

    If you throw an exception, you do not return from the function normally:

    public Integer doSomething()
    {
       throw new my_exception();
       // The following code does NOT get run
       return something;
    }
    
    public void main()
    {
      Integer myValue = doSomething();
    }
    

    The main advantages of exceptions are:

    • You can write your code as though everything is succeeding, which is usually clearer
    • Exceptions are hard to ignore. If an exception is unhandled, typically an obvious and loud error will be given, with a stack trace. This contrasts with error codes, where it is much easier to ignore the error handling than not.

    I recommend this post by Eric Lippert, which discusses exceptions and when it is and is not appropriate to handle them.


    UPDATE (in response to comment):

    You can absolutely handle an exception and continue, you do this by catching the exception.

    eg:

    try
    {
       // Perform calculation
    }
    catch (ExceptionType ex)
    {
       // A problem of type 'ExceptionType' occurred - you can do whatever you
       // want here.
       // You could log it to a list, which will be later shown to the user,
       // you could set a flag to pop up a dialog box later, etc
    }
    
    // The code here will still get run even if ExceptionType was thrown inside
    // the try {} block, because we caught and handled that exception.
    

    The nice thing about this is that you know what kind of thing went wrong (from the exception type), as well as details (by looking into the information in ex), so you
    hopefully have the information you need to do the right thing.


    UPDATE 2 in response to your edit:

    You should handle the exception at the layer where you are able to respond in the way you want. For your example, you are correct, you should not be catching the exception so deep down in the code, since you don’t have access to the UI, etc and you thus can’t really do anything useful.

    How about this version of your example code:

    public void main()
    {
      int myValue = -1; // some default value
      String error = null; // or however you do it in Java (:
      
      try
      {
        getMyValue();
      }
      catch (exception e)
      {
        error = "Error calculating value. Check your input or something.";
      }
    
      if (error != null)
      {
        // Display the error message to the user, or maybe add it to a list of many
        // errors to be displayed later, etc.
        // Note: if we are just adding to a list, we could do that in the catch().
      }
    
      // Run this code regardless of error - will display default value
      // if there was error.
      // Alternatively, we could wrap this in an 'else' if we don't want to
      // display anything in the case of an error.
      MyUIObject whatever = new MyUIObject();
      whatever.displayValue(myValue); // Display the value in the UI or something
    }
    
    public Integer getMyValue()
    {
      // Calculate some value, don't worry about exceptions since we can't
      // do anything useful at this level.
      return value;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Note: This is a general programming question, not specific to any language, but feel
This is a general database question, not related to any particular database or programming
This is a general question. And may not be specific to datagrids. How can
I think this question has not to do with programming in general, but nevertheless
I'm new to web programming in general so this is probably a really simple
This is a general question, but I'll explain my specific need at the moment:
This is a general programming question. What is the best way to make a
This is not an Eclipse-programming question, but rather a question about the Eclipse user-interface
This question concerns the reflection mechanisms of the java programming language. I have an
This is a moot question as I'm not on this project any more, 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.