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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:17:17+00:00 2026-05-22T03:17:17+00:00

Consider this code: x = Repo.GetX(13); Services.Process(x); I could check that x is null

  • 0

Consider this code:

x = Repo.GetX(13);
Services.Process(x);

I could check that x is null before processing it, or I could have Services.Process() throws an exception if X is null and use a try/catch block.

Which method is preferable?

(Or should I do both, just in the exceptional case that x becomes null somehow from the point of its retrieval to processing it?)

  • 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-22T03:17:18+00:00Added an answer on May 22, 2026 at 3:17 am

    If you’re trying to decide between:

    x = Repo.GetX(13);
    try {
        Services.Process(x);
    } catch (NullPointerException) {
        System.err.println('x is NULL');
    }
    

    or

    x = Repo.GetX(13);
    if (x != NULL) {
        Services.Process(x);
    } else {
        System.err.println('x is NULL');
    }
    

    Then be aware that they don’t mean the same thing. The first one catches any NullPointerException thrown by Process. The second one only handles the case where x itself is NULL – if Process does x.foo().bar(), and x.foo() returns NULL, it isn’t handled.

    You should decide between the two based on what errors you can handle.

    In the case where Services.Process is documented to throw NullPointerException if and only if x is NULL, so that (assuming we believe the documentation) the two do mean the same thing, then I’d do the second one. That way, if the documentation is lies, and there’s a bug in Services.Process that makes it throw in some other situation, then we will not erroneously handle/report the exception as though x were NULL when in fact it is not.

    So, catch and suppress an exception in two situations:

    1. You know what (kind of thing) caused the error, and you know that you can handle that situation and continue regardless.
    2. To protect some fairly outer level of code, like an event loop, from unexpected faults in the program or the environment. Ideally this should result in extensive logging, because either the system has failed to do what you expected (perhaps due to lack of resources or unmet dependencies), or else there’s a bug in someone’s code somewhere.

    Don’t catch just because you know one situation that could cause the exception, and can handle that, when the same exception could be thrown for other reasons. I suppose you could do:

    x = Repo.GetX(13);
    try {
        Services.Process(x);
    } catch (NullPointerException) {
        if (x == NULL) {
            System.err.println('x is NULL');
        } else {
            throw;
        }
    }
    

    But that seems to me needlessly verbose in this case: since we can check in advance whether x is null, we might as well. There are cases, though, where you catch an exception because it’s impossible or just silly to try to predict in advance whether it will occur or not:

    String s = Rep.GetString(13);
    try {
        f = Float.parseFloat(s);
        System.out.println('is a number');
    } catch (NumberFormatException) {
        System.out.println('not a number');
    }
    

    We could check whether s is a valid number before calling parseFloat, but that would basically be doing the same work twice – checking a string is a valid float is pretty much the same as actually converting it. So it makes sense to catch this exception – we know what it means, we know what to do about it, we couldn’t realistically have prevented it from happening.

    (Or you could use DecimalFormat.parse, which doesn’t throw at all)

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

Sidebar

Related Questions

Consider this code: char *strs[] = { string1, string2, NULL }; char *ptr1 =
Consider this code example: var source = null; $.ajax({ [...], success:function(data) { source =
Consider this code: function SwapColumns() { chrome.tabs.executeScript(null, {code:SwapNameAndSurname();}); } The code above is in
Consider this code: var unit: Unit = null unit: Unit = () a) Why
Consider this code... using System.Threading; //... Timer someWork = new Timer( delegate(object state) {
consider this code block public void ManageInstalledComponentsUpdate() { IUpdateView view = new UpdaterForm(); BackgroundWorker
Consider this code (Java, specifically): public int doSomething() { doA(); try { doB(); }
Consider this code: new Ajax.Request('?service=example', { parameters : {tags : 'exceptions'}, onSuccess : this.dataReceived.bind(this)
Consider this code: public <T> List<T> meth(List<?> type) { System.out.println(type); // 1 return new
Consider this code: import socket store = [] scount = 0 while True: scount+=1

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.