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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:02:03+00:00 2026-05-13T17:02:03+00:00

Is it better to return a null value or throw an exception from an

  • 0

Is it better to return a null value or throw an exception from an API method?

Returning a null requires ugly null checks all over, and cause a major quality problem if the return is not checked.

Throwing an exception forces the user to code for the faulty condition, but since Java exceptions bubble up and force the caller code to handle them, in general, using custom exceptions may be a bad idea (specifically in java).

Any sound and practical advice?

  • 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-13T17:02:03+00:00Added an answer on May 13, 2026 at 5:02 pm

    I think the answer is entirely dependent on the context of your application, and what you consider to be an “exceptional circumstance” versus programmer error.

    For example, if writing an XML parser you may choose to implement methods like:

    /**
     * Returns first child element with matching element name or else
     * throws an exception.  Will never return null.
     */
    Element getMandatoryChildElement(Element parent, String elementName)
    throws MissingElementException;
    

    … as this approach allows you to implement your message parsing code in a single block of logic, without having to check that the message is well-formed after retrieving each element or attribute.

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

Sidebar

Related Questions

In your opinions, is it better to return a newly allocated memory stream from
What's better practice when defining several methods that return the same shape of data
Are there good reasons why it's a better practice to have only one return
When implementing a singleton in C++, is it better for GetInstance() to return a
The below code in Java throws Null pointer exception. public class New{ int i;
I have this method, that can return three different response. At first, it was
I have an overloaded generic method used to obtain the value of a property
Better yet, how can I make My Computer always open in Explorer as well?
Or better said: When to use array as a field data type in a
Even better would be if autoResize in latest branch would work as intended, 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.