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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:00:47+00:00 2026-06-16T00:00:47+00:00

The Exception is a convenient container, that is tempting to use for various purposes.

  • 0

The Exception is a convenient container, that is tempting to use for various purposes. But is it OK to use it for handling legal states in your code?

My example: I have a geometric function that finds the closest object within a search radius:

public IPoint FindNearest(IPoint origin, double searchRadius)
{

}

My idea was that I could throw an exception, when the search doesn’t find a hit. But is this a good idea? Alternatively, I could return Null (which I don’t like), or return a result object instead of a Point.

  • 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-16T00:00:48+00:00Added an answer on June 16, 2026 at 12:00 am

    Usually in this situation throwing exceptions isn’t a good idea, they are expensive and semantically mean something else entirely.

    You could return null and do a null check, or I occasionally find that using the Special Case pattern works out nicely and makes for readable code if you give the class/interface a sensible name.

    In this instance, you’d return either an implementing class or derived interface called something like:

    public class NoHitOnRadius : IPoint {}
    

    And return that from the call when you get no hits. Then the calling code checks the return type:

    var p = FindNearest(...);
    
    if (p is NoHitOnRadius) 
    {
        // Do something.
    }
    

    Although in this specific situation I’d likely go with the TryFindNearest semantics (to keep commonality with the likes of TryParse etc) that RobH suggests.

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

Sidebar

Related Questions

Argument exception error is thrown at the following code. It's a simple code but
I use AutoBean to code/decode data to JSON and that was all right in
Exception: Object reference not set to an instance of an object. Code: For Each
Exception Handling in java based we services JAX-WS. I want to create user defined
Consider the following nonsense script as an example: use strict; use warnings; my $uninitialisedValue;
I use the code below to fetch the first two pages of google search
We usually catch exception in the upper level of a code like the GUI
I've never been completely happy with the way exception handling works, there's a lot
I was reading some things about exception handling in Java, to be able to
I use Hudson for both CI and restart of Glassfish server. It's convenient as

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.