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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:49:54+00:00 2026-05-11T13:49:54+00:00

So I have an abstract data type called RegionModel with a series of values

  • 0

So I have an abstract data type called RegionModel with a series of values (Region), each mapped to an index. It’s possible to remove a number of regions by calling:

regionModel.removeRegions(index, numberOfRegionsToRemove); 

My question is what’s the best way to handle a call when the index is valid :

(between 0 (inclusive) and the number of Regions in the model (exclusive))

but the numberOfRegionsToRemove is invalid:

(index + regionsToRemove > the number of regions in the model)

Is it best to throw an exception like IllegalArgumentException or just to remove as many Regions as I can (all the regions from index to the end of the model)?

Sub-question: if I throw an exception what’s the recommended way to unit test that the call threw the exception and left the model untouched (I’m using Java and JUnit here but I guess this isn’t a Java specific question).

  • 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-11T13:49:54+00:00Added an answer on May 11, 2026 at 1:49 pm

    I agree with Mitchel and casperOne — an Exception makes the most sense.

    As far as unit testing is concerned, JUnit4 allows you to exceptions directly: http://www.ibm.com/developerworks/java/library/j-junit4.html

    You would need only to pass parameters which are guaranteed to cause the exception, and add the correct annotation (@Test(expected=IllegalArgumentException.class)) to the JUnit test method.

    Edit: As Tom Martin mentioned, JUnit 4 is a decent-sized step away from JUnit 3. It is, however, possible to also test exceptions using JUnit 3. It’s just not as easy.

    One of the ways I’ve tested exceptions is by using a try/catch block within the class itself, and embedding Assert statements within it.

    Here’s a simple example — it’s not complete (e.g. regionModel is assumed to be instantiated), but it should get the idea across:

    public void testRemoveRegionsInvalidInputs() {   int originalSize = regionModel.length();   int index = 0;   int numberOfRegionsToRemove = 1,000; // > than regionModel's current size   try {     regionModel.removeRegions(index, numberOfRegionsToRemove);      // Since the exception will immediately go into the 'catch' block this code will only run if the IllegalArgumentException wasn't thrown     Assert.assertTrue('Exception not Thrown!', false);   }   catch (IllegalArgumentException e) {      Assert.assertTrue('Exception thrown, but regionModel was modified', regionModel.length() == originalSize);   }   catch (Exception e) {       Assert.assertTrue('Incorrect exception thrown', false);   } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a LINQ to SQL class called Data with a column of type
I have an abstract data type that points to a concrete data type structure.
Let say I have abstract class called: Tenant and Customer. The tenant in this
I have an abstract entity called Block which contains two attributes: column and order
I want to have an abstract class Server with an abstract method called Initialize
I am creating an Abstract Data Type, which create a doubly linked list (not
So I'm trying to implement the following; // Destructive Abstract data type ilist struct
Suppose I am writing a substitute function on an abstract syntax tree data type:
My code is using std::count() on a list of an abstract data type that
I have a stored procedure that is called by data collection system . The

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.