How can I execute different catches depending on which side I am out of bounds?C++ API design and error handlingGetting and setting values to an array with Overloading the subscript operator “[ ]” won't workHow would I create an Array class that can have lower bounds other than zero (in C++)?Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviationsOperator “<<” overloading return typeC++ : how to return the value from an out of bounds array with exception handling?try-catch when adding value to ArrayList is out of bound using loop JavaC++ - Odd Reciprocal InequivalenceHow to exit program execution after catching exceptionWhy isn't the following class doing shallow copy for the array arr?
Does throwing a non-weapon item take an action?
What does it mean to move a single flight control to its full deflection?
Why is there no Disney logo in MCU movies?
Can a network vulnerability be exploited locally?
How can I reply to coworkers who accuse me of automating people out of work?
Under GDPR, can I give permission once to allow everyone to store and process my data?
Wrong Stamping of UK Visa
Is "survival" paracord with fire starter strand dangerous
Answer with an image of my favorite musician
I feel cheated on by my new employer, does this sound right?
Why does Sauron not permit his followers to use his name?
Where does the last newline character come from in this sed's result?
Why is "I let him to sleep" incorrect (or is it)?
What does なんだって mean in this case? 「そういう子なんだってだけで...」
Where should I draw the line on follow up questions from previous employer
Create a list of snaking numbers under 50,000
Why didn't Doc believe Marty was from the future?
What's the point of fighting monsters in Zelda BotW?
What should be done with the carbon when using magic to get oxygen from carbon dioxide?
Do application leftovers have any impact on performance?
Heat output from a 200W electric radiator?
Why can't I identify major and minor chords?
Should I ask for a raise one month before the end of an internship?
Why doesn't Starship have four landing legs?
How can I execute different catches depending on which side I am out of bounds?
C++ API design and error handlingGetting and setting values to an array with Overloading the subscript operator “[ ]” won't workHow would I create an Array class that can have lower bounds other than zero (in C++)?Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviationsOperator “<<” overloading return typeC++ : how to return the value from an out of bounds array with exception handling?try-catch when adding value to ArrayList is out of bound using loop JavaC++ - Odd Reciprocal InequivalenceHow to exit program execution after catching exceptionWhy isn't the following class doing shallow copy for the array arr?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I've overloaded an operator such that I can return the value of an array. I can handle out of bounds using if:
float arr::operator[](const int i) const
if (i < 0)
cout << "Outside of array, first entry returned" << endl;
return value[0];
else if (i >=size)
cout << "Outside of array, last entry returned" << endl;
return value[size-1];
else return value[i];
but I am learning about exceptions and try-catch blocks.
Is it possible to throw an exception with a different int (for example) for out of upper/lower bounds, and have catches that execute different code depending on the value of that int?
I know that if works well here, but I'm looking to develop my knowledge for use in more complex ideas.
c++ exception try-catch
add a comment |
I've overloaded an operator such that I can return the value of an array. I can handle out of bounds using if:
float arr::operator[](const int i) const
if (i < 0)
cout << "Outside of array, first entry returned" << endl;
return value[0];
else if (i >=size)
cout << "Outside of array, last entry returned" << endl;
return value[size-1];
else return value[i];
but I am learning about exceptions and try-catch blocks.
Is it possible to throw an exception with a different int (for example) for out of upper/lower bounds, and have catches that execute different code depending on the value of that int?
I know that if works well here, but I'm looking to develop my knowledge for use in more complex ideas.
c++ exception try-catch
1
Don't throw integers. Throw a type.
– 1201ProgramAlarm
Mar 27 at 22:07
Ah yes, I considered this, but it feels clunky? Throw an int for below bounds and a string for above? Is this the best solution?
– William
Mar 27 at 22:13
1
Not clunky at all and instantly recognizable to anyone reading your code. Name the type correctly and the code is almost self documenting. A programmer who seesthrow 42;is likely to have a WTF moment followed by searching through the code trying to figure out what you are doing or the same WTF moment followed tossing your code into a waste bin. But if they seethrow lowerbound;, that tells them something.
– user4581301
Mar 27 at 22:16
Why you don't throw directlystd::out_of_range?
– Raindrop7
Mar 27 at 22:28
@raindrop7 As far as I could see it didn't differentiate low and high, just that it was out of bounds.
– William
Mar 27 at 22:41
add a comment |
I've overloaded an operator such that I can return the value of an array. I can handle out of bounds using if:
float arr::operator[](const int i) const
if (i < 0)
cout << "Outside of array, first entry returned" << endl;
return value[0];
else if (i >=size)
cout << "Outside of array, last entry returned" << endl;
return value[size-1];
else return value[i];
but I am learning about exceptions and try-catch blocks.
Is it possible to throw an exception with a different int (for example) for out of upper/lower bounds, and have catches that execute different code depending on the value of that int?
I know that if works well here, but I'm looking to develop my knowledge for use in more complex ideas.
c++ exception try-catch
I've overloaded an operator such that I can return the value of an array. I can handle out of bounds using if:
float arr::operator[](const int i) const
if (i < 0)
cout << "Outside of array, first entry returned" << endl;
return value[0];
else if (i >=size)
cout << "Outside of array, last entry returned" << endl;
return value[size-1];
else return value[i];
but I am learning about exceptions and try-catch blocks.
Is it possible to throw an exception with a different int (for example) for out of upper/lower bounds, and have catches that execute different code depending on the value of that int?
I know that if works well here, but I'm looking to develop my knowledge for use in more complex ideas.
c++ exception try-catch
c++ exception try-catch
asked Mar 27 at 22:01
WilliamWilliam
576 bronze badges
576 bronze badges
1
Don't throw integers. Throw a type.
– 1201ProgramAlarm
Mar 27 at 22:07
Ah yes, I considered this, but it feels clunky? Throw an int for below bounds and a string for above? Is this the best solution?
– William
Mar 27 at 22:13
1
Not clunky at all and instantly recognizable to anyone reading your code. Name the type correctly and the code is almost self documenting. A programmer who seesthrow 42;is likely to have a WTF moment followed by searching through the code trying to figure out what you are doing or the same WTF moment followed tossing your code into a waste bin. But if they seethrow lowerbound;, that tells them something.
– user4581301
Mar 27 at 22:16
Why you don't throw directlystd::out_of_range?
– Raindrop7
Mar 27 at 22:28
@raindrop7 As far as I could see it didn't differentiate low and high, just that it was out of bounds.
– William
Mar 27 at 22:41
add a comment |
1
Don't throw integers. Throw a type.
– 1201ProgramAlarm
Mar 27 at 22:07
Ah yes, I considered this, but it feels clunky? Throw an int for below bounds and a string for above? Is this the best solution?
– William
Mar 27 at 22:13
1
Not clunky at all and instantly recognizable to anyone reading your code. Name the type correctly and the code is almost self documenting. A programmer who seesthrow 42;is likely to have a WTF moment followed by searching through the code trying to figure out what you are doing or the same WTF moment followed tossing your code into a waste bin. But if they seethrow lowerbound;, that tells them something.
– user4581301
Mar 27 at 22:16
Why you don't throw directlystd::out_of_range?
– Raindrop7
Mar 27 at 22:28
@raindrop7 As far as I could see it didn't differentiate low and high, just that it was out of bounds.
– William
Mar 27 at 22:41
1
1
Don't throw integers. Throw a type.
– 1201ProgramAlarm
Mar 27 at 22:07
Don't throw integers. Throw a type.
– 1201ProgramAlarm
Mar 27 at 22:07
Ah yes, I considered this, but it feels clunky? Throw an int for below bounds and a string for above? Is this the best solution?
– William
Mar 27 at 22:13
Ah yes, I considered this, but it feels clunky? Throw an int for below bounds and a string for above? Is this the best solution?
– William
Mar 27 at 22:13
1
1
Not clunky at all and instantly recognizable to anyone reading your code. Name the type correctly and the code is almost self documenting. A programmer who sees
throw 42; is likely to have a WTF moment followed by searching through the code trying to figure out what you are doing or the same WTF moment followed tossing your code into a waste bin. But if they see throw lowerbound;, that tells them something.– user4581301
Mar 27 at 22:16
Not clunky at all and instantly recognizable to anyone reading your code. Name the type correctly and the code is almost self documenting. A programmer who sees
throw 42; is likely to have a WTF moment followed by searching through the code trying to figure out what you are doing or the same WTF moment followed tossing your code into a waste bin. But if they see throw lowerbound;, that tells them something.– user4581301
Mar 27 at 22:16
Why you don't throw directly
std::out_of_range ?– Raindrop7
Mar 27 at 22:28
Why you don't throw directly
std::out_of_range ?– Raindrop7
Mar 27 at 22:28
@raindrop7 As far as I could see it didn't differentiate low and high, just that it was out of bounds.
– William
Mar 27 at 22:41
@raindrop7 As far as I could see it didn't differentiate low and high, just that it was out of bounds.
– William
Mar 27 at 22:41
add a comment |
1 Answer
1
active
oldest
votes
Is it possible to throw an exception with a different int (for example) for out of upper/lower bounds, and have catches that execute different code depending on the value of that int?
Sort of. You can certainly throw a different int value based on a condition, but you would have to catch a single generic int and test its value, you can't catch different int values individually. For example:
const int OutOfBoundsInFront = -1;
const int OutOfBoundsInBack = 1;
float arr::operator[](const int i) const
if (i < 0)
throw OutOfBoundsInFront;
if (i >= size)
throw OutOfBoundsInBack;
return value[i];
...
try
... = myarr[index];
catch (int value)
switch (value)
case OutOfBoundsInFront:
//...
break;
case OutOfBoundsInBack:
//...
break;
However, when throwing an exception, it is better to throw an object instead of a simple POD type. catch blocks deal in types, not values. In this case, you can define different class types for each condition you want to catch. For example:
#include <stdexcept>
class OutOfBoundsInFront : public std::out_of_range
public:
OutOfBoundsInFront() : std::out_of_range("out of bounds in front")
;
class OutOfBoundsInBack : public std::out_of_range
public:
OutOfBoundsInBack() : std::out_of_range("out of bounds in back")
;
float arr::operator[](const int i) const
if (i < 0)
throw OutOfBoundsInFront();
if (i >= size)
throw OutOfBoundsInBack();
return value[i];
...
try
... = myarr[index];
catch (const OutOfBoundsInFront &)
//...
catch (const OutOfBoundsInBack &)
//...
Alternatively:
#include <stdexcept>
class OutOfBoundsOnSide : public std::out_of_range
public:
enum WhichSide InFront, InBack ;
static const char* WhichSideErrorMsg[] =
"out of bounds in front",
"out of bounds in back"
;
WhichSide whichSide;
OutOfBoundsOnSide(WhichSide side) : std::out_of_range(WhichSideErrorMsg[side]), whichSide(side)
;
class OutOfBoundsInFront : public OutOfBoundsOnSide
public
OutOfBoundsInFront() : OutOfBoundsOnSide(InFront)
;
class OutOfBoundsInBack : public OutOfBoundsOnSide
public
OutOfBoundsInBack() : OutOfBoundsOnSide(InBack)
;
float arr::operator[](const int i) const
if (i < 0)
throw OutOfBoundsInFront();
if (i >= size)
throw OutOfBoundsInBack();
return value[i];
...
try
... = myarr[index];
catch (const OutOfBoundsOnSide &e)
switch (e.whichSide)
case InFront:
//...
break;
case InBack:
//...
break;
Hey, this seems to be an excellent answer! Several solutions building in complexity. I just learnt a lot! (Also, you were always my favourite of the X-Men ;) )
– William
Mar 27 at 22:40
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55387147%2fhow-can-i-execute-different-catches-depending-on-which-side-i-am-out-of-bounds%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Is it possible to throw an exception with a different int (for example) for out of upper/lower bounds, and have catches that execute different code depending on the value of that int?
Sort of. You can certainly throw a different int value based on a condition, but you would have to catch a single generic int and test its value, you can't catch different int values individually. For example:
const int OutOfBoundsInFront = -1;
const int OutOfBoundsInBack = 1;
float arr::operator[](const int i) const
if (i < 0)
throw OutOfBoundsInFront;
if (i >= size)
throw OutOfBoundsInBack;
return value[i];
...
try
... = myarr[index];
catch (int value)
switch (value)
case OutOfBoundsInFront:
//...
break;
case OutOfBoundsInBack:
//...
break;
However, when throwing an exception, it is better to throw an object instead of a simple POD type. catch blocks deal in types, not values. In this case, you can define different class types for each condition you want to catch. For example:
#include <stdexcept>
class OutOfBoundsInFront : public std::out_of_range
public:
OutOfBoundsInFront() : std::out_of_range("out of bounds in front")
;
class OutOfBoundsInBack : public std::out_of_range
public:
OutOfBoundsInBack() : std::out_of_range("out of bounds in back")
;
float arr::operator[](const int i) const
if (i < 0)
throw OutOfBoundsInFront();
if (i >= size)
throw OutOfBoundsInBack();
return value[i];
...
try
... = myarr[index];
catch (const OutOfBoundsInFront &)
//...
catch (const OutOfBoundsInBack &)
//...
Alternatively:
#include <stdexcept>
class OutOfBoundsOnSide : public std::out_of_range
public:
enum WhichSide InFront, InBack ;
static const char* WhichSideErrorMsg[] =
"out of bounds in front",
"out of bounds in back"
;
WhichSide whichSide;
OutOfBoundsOnSide(WhichSide side) : std::out_of_range(WhichSideErrorMsg[side]), whichSide(side)
;
class OutOfBoundsInFront : public OutOfBoundsOnSide
public
OutOfBoundsInFront() : OutOfBoundsOnSide(InFront)
;
class OutOfBoundsInBack : public OutOfBoundsOnSide
public
OutOfBoundsInBack() : OutOfBoundsOnSide(InBack)
;
float arr::operator[](const int i) const
if (i < 0)
throw OutOfBoundsInFront();
if (i >= size)
throw OutOfBoundsInBack();
return value[i];
...
try
... = myarr[index];
catch (const OutOfBoundsOnSide &e)
switch (e.whichSide)
case InFront:
//...
break;
case InBack:
//...
break;
Hey, this seems to be an excellent answer! Several solutions building in complexity. I just learnt a lot! (Also, you were always my favourite of the X-Men ;) )
– William
Mar 27 at 22:40
add a comment |
Is it possible to throw an exception with a different int (for example) for out of upper/lower bounds, and have catches that execute different code depending on the value of that int?
Sort of. You can certainly throw a different int value based on a condition, but you would have to catch a single generic int and test its value, you can't catch different int values individually. For example:
const int OutOfBoundsInFront = -1;
const int OutOfBoundsInBack = 1;
float arr::operator[](const int i) const
if (i < 0)
throw OutOfBoundsInFront;
if (i >= size)
throw OutOfBoundsInBack;
return value[i];
...
try
... = myarr[index];
catch (int value)
switch (value)
case OutOfBoundsInFront:
//...
break;
case OutOfBoundsInBack:
//...
break;
However, when throwing an exception, it is better to throw an object instead of a simple POD type. catch blocks deal in types, not values. In this case, you can define different class types for each condition you want to catch. For example:
#include <stdexcept>
class OutOfBoundsInFront : public std::out_of_range
public:
OutOfBoundsInFront() : std::out_of_range("out of bounds in front")
;
class OutOfBoundsInBack : public std::out_of_range
public:
OutOfBoundsInBack() : std::out_of_range("out of bounds in back")
;
float arr::operator[](const int i) const
if (i < 0)
throw OutOfBoundsInFront();
if (i >= size)
throw OutOfBoundsInBack();
return value[i];
...
try
... = myarr[index];
catch (const OutOfBoundsInFront &)
//...
catch (const OutOfBoundsInBack &)
//...
Alternatively:
#include <stdexcept>
class OutOfBoundsOnSide : public std::out_of_range
public:
enum WhichSide InFront, InBack ;
static const char* WhichSideErrorMsg[] =
"out of bounds in front",
"out of bounds in back"
;
WhichSide whichSide;
OutOfBoundsOnSide(WhichSide side) : std::out_of_range(WhichSideErrorMsg[side]), whichSide(side)
;
class OutOfBoundsInFront : public OutOfBoundsOnSide
public
OutOfBoundsInFront() : OutOfBoundsOnSide(InFront)
;
class OutOfBoundsInBack : public OutOfBoundsOnSide
public
OutOfBoundsInBack() : OutOfBoundsOnSide(InBack)
;
float arr::operator[](const int i) const
if (i < 0)
throw OutOfBoundsInFront();
if (i >= size)
throw OutOfBoundsInBack();
return value[i];
...
try
... = myarr[index];
catch (const OutOfBoundsOnSide &e)
switch (e.whichSide)
case InFront:
//...
break;
case InBack:
//...
break;
Hey, this seems to be an excellent answer! Several solutions building in complexity. I just learnt a lot! (Also, you were always my favourite of the X-Men ;) )
– William
Mar 27 at 22:40
add a comment |
Is it possible to throw an exception with a different int (for example) for out of upper/lower bounds, and have catches that execute different code depending on the value of that int?
Sort of. You can certainly throw a different int value based on a condition, but you would have to catch a single generic int and test its value, you can't catch different int values individually. For example:
const int OutOfBoundsInFront = -1;
const int OutOfBoundsInBack = 1;
float arr::operator[](const int i) const
if (i < 0)
throw OutOfBoundsInFront;
if (i >= size)
throw OutOfBoundsInBack;
return value[i];
...
try
... = myarr[index];
catch (int value)
switch (value)
case OutOfBoundsInFront:
//...
break;
case OutOfBoundsInBack:
//...
break;
However, when throwing an exception, it is better to throw an object instead of a simple POD type. catch blocks deal in types, not values. In this case, you can define different class types for each condition you want to catch. For example:
#include <stdexcept>
class OutOfBoundsInFront : public std::out_of_range
public:
OutOfBoundsInFront() : std::out_of_range("out of bounds in front")
;
class OutOfBoundsInBack : public std::out_of_range
public:
OutOfBoundsInBack() : std::out_of_range("out of bounds in back")
;
float arr::operator[](const int i) const
if (i < 0)
throw OutOfBoundsInFront();
if (i >= size)
throw OutOfBoundsInBack();
return value[i];
...
try
... = myarr[index];
catch (const OutOfBoundsInFront &)
//...
catch (const OutOfBoundsInBack &)
//...
Alternatively:
#include <stdexcept>
class OutOfBoundsOnSide : public std::out_of_range
public:
enum WhichSide InFront, InBack ;
static const char* WhichSideErrorMsg[] =
"out of bounds in front",
"out of bounds in back"
;
WhichSide whichSide;
OutOfBoundsOnSide(WhichSide side) : std::out_of_range(WhichSideErrorMsg[side]), whichSide(side)
;
class OutOfBoundsInFront : public OutOfBoundsOnSide
public
OutOfBoundsInFront() : OutOfBoundsOnSide(InFront)
;
class OutOfBoundsInBack : public OutOfBoundsOnSide
public
OutOfBoundsInBack() : OutOfBoundsOnSide(InBack)
;
float arr::operator[](const int i) const
if (i < 0)
throw OutOfBoundsInFront();
if (i >= size)
throw OutOfBoundsInBack();
return value[i];
...
try
... = myarr[index];
catch (const OutOfBoundsOnSide &e)
switch (e.whichSide)
case InFront:
//...
break;
case InBack:
//...
break;
Is it possible to throw an exception with a different int (for example) for out of upper/lower bounds, and have catches that execute different code depending on the value of that int?
Sort of. You can certainly throw a different int value based on a condition, but you would have to catch a single generic int and test its value, you can't catch different int values individually. For example:
const int OutOfBoundsInFront = -1;
const int OutOfBoundsInBack = 1;
float arr::operator[](const int i) const
if (i < 0)
throw OutOfBoundsInFront;
if (i >= size)
throw OutOfBoundsInBack;
return value[i];
...
try
... = myarr[index];
catch (int value)
switch (value)
case OutOfBoundsInFront:
//...
break;
case OutOfBoundsInBack:
//...
break;
However, when throwing an exception, it is better to throw an object instead of a simple POD type. catch blocks deal in types, not values. In this case, you can define different class types for each condition you want to catch. For example:
#include <stdexcept>
class OutOfBoundsInFront : public std::out_of_range
public:
OutOfBoundsInFront() : std::out_of_range("out of bounds in front")
;
class OutOfBoundsInBack : public std::out_of_range
public:
OutOfBoundsInBack() : std::out_of_range("out of bounds in back")
;
float arr::operator[](const int i) const
if (i < 0)
throw OutOfBoundsInFront();
if (i >= size)
throw OutOfBoundsInBack();
return value[i];
...
try
... = myarr[index];
catch (const OutOfBoundsInFront &)
//...
catch (const OutOfBoundsInBack &)
//...
Alternatively:
#include <stdexcept>
class OutOfBoundsOnSide : public std::out_of_range
public:
enum WhichSide InFront, InBack ;
static const char* WhichSideErrorMsg[] =
"out of bounds in front",
"out of bounds in back"
;
WhichSide whichSide;
OutOfBoundsOnSide(WhichSide side) : std::out_of_range(WhichSideErrorMsg[side]), whichSide(side)
;
class OutOfBoundsInFront : public OutOfBoundsOnSide
public
OutOfBoundsInFront() : OutOfBoundsOnSide(InFront)
;
class OutOfBoundsInBack : public OutOfBoundsOnSide
public
OutOfBoundsInBack() : OutOfBoundsOnSide(InBack)
;
float arr::operator[](const int i) const
if (i < 0)
throw OutOfBoundsInFront();
if (i >= size)
throw OutOfBoundsInBack();
return value[i];
...
try
... = myarr[index];
catch (const OutOfBoundsOnSide &e)
switch (e.whichSide)
case InFront:
//...
break;
case InBack:
//...
break;
edited Mar 27 at 22:49
answered Mar 27 at 22:18
Remy LebeauRemy Lebeau
361k21 gold badges287 silver badges486 bronze badges
361k21 gold badges287 silver badges486 bronze badges
Hey, this seems to be an excellent answer! Several solutions building in complexity. I just learnt a lot! (Also, you were always my favourite of the X-Men ;) )
– William
Mar 27 at 22:40
add a comment |
Hey, this seems to be an excellent answer! Several solutions building in complexity. I just learnt a lot! (Also, you were always my favourite of the X-Men ;) )
– William
Mar 27 at 22:40
Hey, this seems to be an excellent answer! Several solutions building in complexity. I just learnt a lot! (Also, you were always my favourite of the X-Men ;) )
– William
Mar 27 at 22:40
Hey, this seems to be an excellent answer! Several solutions building in complexity. I just learnt a lot! (Also, you were always my favourite of the X-Men ;) )
– William
Mar 27 at 22:40
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55387147%2fhow-can-i-execute-different-catches-depending-on-which-side-i-am-out-of-bounds%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Don't throw integers. Throw a type.
– 1201ProgramAlarm
Mar 27 at 22:07
Ah yes, I considered this, but it feels clunky? Throw an int for below bounds and a string for above? Is this the best solution?
– William
Mar 27 at 22:13
1
Not clunky at all and instantly recognizable to anyone reading your code. Name the type correctly and the code is almost self documenting. A programmer who sees
throw 42;is likely to have a WTF moment followed by searching through the code trying to figure out what you are doing or the same WTF moment followed tossing your code into a waste bin. But if they seethrow lowerbound;, that tells them something.– user4581301
Mar 27 at 22:16
Why you don't throw directly
std::out_of_range?– Raindrop7
Mar 27 at 22:28
@raindrop7 As far as I could see it didn't differentiate low and high, just that it was out of bounds.
– William
Mar 27 at 22:41