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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:18:28+00:00 2026-06-17T10:18:28+00:00

For example, I need to get a Foo object using getFoo() method: Foo foo

  • 0

For example, I need to get a Foo object using getFoo() method:

Foo foo = getFoo();

After this, I want to handle the case that getFoo() didn’t return a Foo object successfully. This in turn includes two cases:

  1. getFoo() throws an exception.
  2. The return result of getFoo() is null, empty String, 0, or whatever should be treated as “Empty result”.

Ideally I would want getFoo() throw an exception instead of returning an empty result, but sometimes that may be a call to a method in another package which I don’t own.

These are methods that I can think of:

1. Duplicate handling code

Foo foo = null;
try {
    foo = getFoo();
} catch (ResourceNotFoundException ex) {
    logger.warn("blablabla");
    foo = defaultFoo;
    someOtherComplicatedStuff();
}
if (null == foo) {
    logger.warn("blablabla");
    foo = defaultFoo;
    someOtherComplicatedStuff();
}

This is definitely not a good idea.

2. Move handling logic to an individual function

Foo foo = null;
try {
    foo = getFoo();
} catch (ResourceNotFoundException ex) {
    handleNullFoo(var1, var2, var3, var4);
}
if (null == foo) {
    handleNullFoo(var1, var2, var3, var4);
}

The problem is the logic may use a lot of local variables. Passing these variables to the external function doesn’t make much sense. Worse, some of the variables may need to be modified during this process. I don’t think modifying arguments in a function is a good practice, not to mention the issue it may cause if an argument is primitive typed.

3. Throw a fake exception

Foo foo = null;
try {
    foo = getFoo();
    if (null == foo) {
        throw new ResourceNotFoundException();
    }
} catch (ResourceNotFoundException ex) {
    logger.warn("blablabla");
    foo = defaultFoo;
    someOtherComplicatedStuff();
}

This is by far the cleanest way, but it is more like a kind of hack. It may not proper to throw a ResourceNotFoundException in a package the exception class doesn’t belong to. It also decreases the readability of the code as people may not realize that the exception is handled inside the function.

So, what’s your suggestion?

Thank you very much!

  • 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-17T10:18:29+00:00Added an answer on June 17, 2026 at 10:18 am

    Here is another alternative:

    Foo foo = null;
    try {
        foo = getFoo();
    } catch (ResourceNotFoundException ex) {
        //TODO: log exception?
        foo = null;
    }
    
    if (null == foo) {
        logger.warn("blablabla");
        foo = defaultFoo;
        someOtherComplicatedStuff();
    }
    

    Note that the foo = null; in the catch clause is almost certainly unnecessary. It is only needed if there’s some code after foo = getFoo() that can throw ResourceNotFoundException.

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

Sidebar

Related Questions

For example i have this xml. I need to get value of parameter val
We can use reflection to get method names as follows: object Foo { def
I need to get the decimal part out of a float number, for example:
I need to get some data from a column. For example I have data
I need to get the IDs around an ID with mySQL. Example: IDs ​​in
In javascript I need to get the text within the custom tag. For example
I have an example of what I need decoded: @f&#252r How can I get
For example I need to disable two buttons in runtime. After I disabled first
I've got a need to create a dynamic proxy in C#. I want this
Hello i need to use writerreaderlock in my method. I want to know how

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.