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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:58:52+00:00 2026-05-31T16:58:52+00:00

I have this piece of code: private void myFunc(){ obj = doSomething(); //If value

  • 0

I have this piece of code:

private void myFunc(){
   obj = doSomething();
   //If value is found, doX()
   //If value is not found, doY()
}

private obj doSomething(){
  //Do some stuff
  //Hit database to get some value
  obj = db.getValue(); //This func throws an exception if no value is found
}

Now, my question is:
1. Should I do: doSomething() throws ValueNotFoundException and catch it in myFunc()
2. Or catch it in doSomething() and return null? (a very bad approach though). And then check for null in myFunc()

Update:
1. Value not found is something which is probable and not an error.

  • 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-31T16:58:53+00:00Added an answer on May 31, 2026 at 4:58 pm

    Well, is value not found something exceptional (indicating an error) or probable? If it is absolutely not possible that doSomething() can’t find what it needs, this is an error. I guess myFunc() is not the right place to handle such error.

    If doSomething() sometimes can’t find this or that (because the input is incorrect, misconfiguration, etc.) then it should not throw an exception and let the client handle this expected situation. However null is not the best return value. Instead consider null-object design pattern or some wrapper like Option[T] in Scala.

    This is one of the reasons why InputStream is not simply throwing EOFException when it reaches end of file. This is not an unexpected situation.

    In Java I try to follow some techniques/naming conventions to make null more obvious:

    private Obj tryReturningSomething()
    private Obj returnSomethingOrNull()
    

    Also you can always use JavaDoc to document possible return value. I agree returning null is not the best approach (e.g. when method returns a collection, I always return an empty one instead of null) but in your case it is still better than throwing an exception to be caught one stack frame above. This is wasteful, harder to maintain and read. Consider having two version – one returning null and one throwing an exception, wrapping the first one.

    Exception handling was invented to handle errors, not to control the program flow.

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

Sidebar

Related Questions

I have this piece of code in c#: private static void _constructRow(SqlDataReader reader, system.IO.StreamWriter
I have this piece of code: private void prepareContent() { log.info(do something); // success?
I have this small piece of code. private void Application_Startup(object sender, StartupEventArgs e) {
If I have a piece of code such as this: private void DoOperations(//method passed
I have this piece of LINQ code private static void Original() { using (var
I have this piece of code in my Silverlight project: private void button1_Click(object sender,
I have this piece of code that does not work: public CartaoCidadao() { InitializeComponent();
I have written this piece of code public class Test{ public static void main(String[]
I have this piece of code (summarized)... AnsiString working(AnsiString format,...) { va_list argptr; AnsiString
I have this piece of code #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h>

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.