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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:20:56+00:00 2026-05-11T01:20:56+00:00

Consider this code (Java, specifically): public int doSomething() { doA(); try { doB(); }

  • 0

Consider this code (Java, specifically):

public int doSomething() {     doA();      try {         doB();     } catch (MyException e) {         return ERROR;     }      doC();     return SUCCESS; } 

Where doB() is defined as:

private void doB() throws MyException 

Basically, MyException exists only in case doB() meets some condition (which is not catastrophic, but does need to somehow raise this condition) so that doSomething() will know to exit with an error.

Do you find the use of an exception, in this case to control flow, acceptable? Or is this a code smell? If so, how would you refactor this?

  • 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. 2026-05-11T01:20:56+00:00Added an answer on May 11, 2026 at 1:20 am

    It entirely depends on what that error condition is, and what the method’s job is. If returning ERROR is a valid way of handling that error for the calling function, why would it be bad?

    Often, however, it is a smell. Consider this:

    bool isDouble(string someString) {     try {         double d = Convert.ParseInt32(someString);     } catch(FormatException e) {         return false;     }     return true; } 

    That is a very big code smell, because you don’t expect a double value. You just want to know whether a string contains a double.

    Sometimes, the framework you use doesn’t have other ways of doing what you want. For the above, there is a better way:

    bool isDouble(string someString) {     bool success;     Convert.TryParseInt32(someString, ref success);     return success; } 

    Those kinds of exceptions have a special name, coined by someone whose blog I read recently, but sadly, I forgot its name. Please comment if you know it. Last but not least, the above is pseudocode. I’m not a C# developer so the above doesn’t compile, I’m sure, but TryParseInt32 / ParseInt32 demonstrates that well I think, so I’m going with C#.

    Now, to your code. Let’s inspect two functions. One smells, and the other doesn’t:

    1. Smell

    public int setupSystem() {     doA();      try { doB(); }     catch (MyException e)     { return ERROR; }       doC();     return SUCCESS; } 

    That’s a code smell, because when you want to setup a system, you don’t want it to fail. Failing to setup a system means you can’t continue without handling that error.

    2. OK

    public int pingWorkstation() {     doA();      try { doB(); }     catch (MyException e)     { return ERROR; }       doC();     return SUCCESS; } 

    That is OK, because the purpose of that method is to test whether the workstation is still reachable. If it’s not, then that is part of the result of that method, and not an exceptional case that needs an alternative return path.

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

Sidebar

Ask A Question

Stats

  • Questions 63k
  • Answers 63k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Very interesting questions! I tend to limit my config files… May 11, 2026 at 10:21 am
  • added an answer I assume you're using this question for an interview to… May 11, 2026 at 10:21 am
  • added an answer The trick with these kinds of situations is to turn… May 11, 2026 at 10:21 am

Related Questions

Consider this code (Java, specifically): public int doSomething() { doA(); try { doB(); }
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 sample code: <div class=containter id=ControlGroupDiv> <input onbeforeupdate=alert('bingo 0'); return false; onclick=alert('click 0');return
Do you consider this a code smell? foreach((array)$foo as $bar) { $bar->doStuff(); } Should
Consider: print $foo, AAAAAAAA, $foo, BBBBBBBB; Let's say I want to use this code
Consider the following code: $(a).attr(disabled, disabled); In IE and FF, this will make anchors
Consider this problem: I have a program which should fetch (let's say) 100 records
Consider this case: dll = LoadDLL() dll->do() ... void do() { char *a =
Consider this Python program which uses PyGtk and Hippo Canvas to display a clickable

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.