You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The reason will be displayed to describe this comment to others. Learn more.
at throws an exception, this is an assert which is something different. Most importantly it shows different intentions. But it also easier to debug sich it since it will fail immidateley with line number and possibly a stacktrace instead of just printing a 'caught a out of range exception'
The reason will be displayed to describe this comment to others. Learn more.
@gfgtdf I fail to see the advantage of an assert over an exception. Both cause the program to terminate with an error message, after all; and the exception can be caught if necessary, while the assert can't.
I guess it's true that an exception won't include a line number in the message (but really, having a line number won't help you that much here anyway), but you can get a stack trace from an exception about as easily as you can get one from an assert (assuming you have a debugger attached, you basically don't need to do anything). The only downside is that Wesnoth currently catches all exceptions on exit, obscuring this information (perhaps it shouldn't do this?).
The reason will be displayed to describe this comment to others. Learn more.
@gfgtdf I fail to see the advantage of an assert over an exception. Both cause the program to terminate with an error message, after all
This is not guaranteed for a throw, a random catch somewhere in the stacktrace might prevent it from doing that, in particular, since out_of_range is a quite generic exception that is thown by many functions. One code might for exampel have stuff like map_[sides->at(index).leader().location()] in a catch ( out_of_range) intending to catch for the case that index it out of range ...
and the exception can be caught if necessary, while the assert can't.
This is exactly why we are not using it here, we want to add debug information so we add debug information, if we at some point are in some situation where it makes sense to change the interface of that function to 'allow' out of range values we can still add such a check later.
The reason will be displayed to describe this comment to others. Learn more.
It makes sense to have a fallback if WML does something stupid. If there's a logic error in the code though, we want it to fail as loudly as possible, so it will actually get fixed.
8dc1613
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would prefer if you used
std::map::at
.8dc1613
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
at throws an exception, this is an assert which is something different. Most importantly it shows different intentions. But it also easier to debug sich it since it will fail immidateley with line number and possibly a stacktrace instead of just printing a 'caught a out of range exception'
8dc1613
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know. I just think it's cleaner to use
at
here, especially since you can add relevant error messages.8dc1613
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does't really matter tho.
8dc1613
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gfgtdf I fail to see the advantage of an assert over an exception. Both cause the program to terminate with an error message, after all; and the exception can be caught if necessary, while the assert can't.
I guess it's true that an exception won't include a line number in the message (but really, having a line number won't help you that much here anyway), but you can get a stack trace from an exception about as easily as you can get one from an assert (assuming you have a debugger attached, you basically don't need to do anything). The only downside is that Wesnoth currently catches all exceptions on exit, obscuring this information (perhaps it shouldn't do this?).
8dc1613
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Vultraz What do you mean you can add relevant error messages with
at
? Pretty sure there's no way to do that?8dc1613
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not guaranteed for a throw, a random catch somewhere in the stacktrace might prevent it from doing that, in particular, since out_of_range is a quite generic exception that is thown by many functions. One code might for exampel have stuff like
map_[sides->at(index).leader().location()]
in acatch ( out_of_range)
intending to catch for the case that index it out of range ...This is exactly why we are not using it here, we want to add debug information so we add debug information, if we at some point are in some situation where it makes sense to change the interface of that function to 'allow' out of range values we can still add such a check later.
EDIT: in fact we do such things, see https://github.com/wesnoth/wesnoth/blob/1.13.11/src/pathfind/pathfind.cpp#L544
8dc1613
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@CelticMinstrel I was thinking along the lines of:
8dc1613
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It makes sense to have a fallback if WML does something stupid. If there's a logic error in the code though, we want it to fail as loudly as possible, so it will actually get fixed.