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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:46:22+00:00 2026-05-26T17:46:22+00:00

I have a function as part of a rectangle class (points inside it are

  • 0

I have a function as part of a rectangle class (points inside it are immutable doubles, so the rectangle is also immutable), and I need to provide a method for calculating intersections with another rectangle. The method will return the calculated intersection rectangle. However, if the objects do not intersect at all, it throws an exception.

The only alternative to throwing an exception I could think of would be to return a special type called RectIntersection, and the caller could poll that object to see if the intersection calculation failed or not. I like this better than throwing an exception, but it leaves me needing to test every call to this function to check the newly created object.

Any other suggestions for handling this condition?

static public DoubleRect calcRectIntersection(DoubleRect r1, DoubleRect r2) throws DoubleRectException {

    if((r1.topLeft.x > r2.bottomRight.x || r1.bottomRight.x < r2.topLeft.x || r1.topLeft.y > r2.bottomRight.y || r1.bottomRight.y < r2.topLeft.y) != true)
    {
        return new DoubleRect(r1.topLeft.x >= r2.topLeft.x ? r1.topLeft.x : r2.topLeft.x,
                r1.topLeft.y >= r2.topLeft.y ? r1.topLeft.y : r2.topLeft.y,
                r1.bottomRight.x <= r2.bottomRight.x ? r1.bottomRight.x : r2.bottomRight.x,
                r1.bottomRight.y <= r2.bottomRight.y ? r1.bottomRight.y : r2.bottomRight.y);
    }
    else throw new DoubleRectException("Call to calcRectIntersection() could not complete since the two rectangles did not intersect");
}
  • 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-26T17:46:23+00:00Added an answer on May 26, 2026 at 5:46 pm

    Why not just return null? You just check to make sure the return value isn’t null before using it.

    Or, as Oli noted, return an empty rectangle. Arguably, that’s what the intersection of two nonoverlapping rectangles is anyway. You may not even need to modify the code that uses it (it’s easy to forsee code that does nothing given an empty rect), or you can add an isEmpty method to check the result. There’s precdent for this, in the Rectangle class

    You’re right, exceptions aren’t really the tool to use for nonexceptional conditions.

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

Sidebar

Related Questions

I have a function which is part of a class and in this function
I have a function to load a html part into a element in main
We have this anonymous function in our code, which is part of the jQuery's
I have a function in my class that creates a thread and gives it
I have this function $.ajax({ url: aurl, method: 'GET', beforeSend: function (req) { req.setRequestHeader('Authorization',
I have a fitness function as part of a lab and wish to apply
I have a function that fills a part of the bitmap created by imagecreatetruecolor($h,$w)
I have code for get part of anchor: function _get_part(queryString, name) { var match
I have an third part dll that have a function that returns a string.
As part of my application I have a function that receives a MethodInfo and

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.