Exceptions

Source Code and development in general from a technical point of view. Post Patches here.

Moderator: SMC Team

Exceptions

Postby Quintus » 06 May 2012 18:25

Hi there,

any specific reason why SMC uses exceptions so rarely? I mean, when a problem has been detected that will corrupt the game’s state further on, isn’t it better to terminate the program instead of just printing out a warning an rectifying whatever was wrong afterwards? When such a condition occurs, the game should IMHO crash with some useful information as it most likely is a bug that needs to be fixed.

Valete,
Quintus
Come to our fork of SMC: https://secretchronicles.org
Quintus
SMC Team
SMC Team
 
Posts: 354
Joined: 14 Sep 2010 18:05

Re: Exceptions

Postby BowserJr » 07 May 2012 09:39

Might be a general coding standards thing. I've seen plenty of people discouraging using exceptions in C++, or indeed any language, but much more so in C++ because of how they're implemented. Pretty much all the code was written by one person, so how he was taught will have a big influence.

But yeah, more informative error messages/ stack traces would be a big help.
"Plants need to have water poured on them because they have no hands to hold glasses of water."
User avatar
BowserJr
SMC Team
SMC Team
 
Posts: 1543
Joined: 05 Feb 2007 14:07
Location: London, UK

Re: Exceptions

Postby Quintus » 07 May 2012 11:45

BowserJr wrote:Might be a general coding standards thing. I've seen plenty of people discouraging using exceptions in C++, or indeed any language, but much more so in C++ because of how they're implemented.
I see the point, but given that one doesn’t usually use exceptions as a mean to control general program flow, but rather to signal that something went wrong, i.e. an exceptional situation has occured, it appears OK to me to break out of the control flow as someone has to deal with the error -- if noone does, the program should crash with an unhandled exception error.

What exactly do you mean by "how they’re implemented"? AFAIK each compiler has its own way to implement things, and I’m pretty sure that g++ implements exceptions in another way than MSVC. If you refer to the fact that exceptions interrupt program flow I’d like to say that this is the very purpose of exceptions (as long as one doesn’t abuse them for example to break out of nested loops). Throwing an exception is also a perfomance-expensive operation, but when a program is in a state where continuing to run is impossible anyway, this can be ignored IMO.

BowserJr wrote:But yeah, more informative error messages/ stack traces would be a big help.
So it’s OK if I use exceptions for the code I add to SMC? I won’t skim through SMC whole code and add exceptions everywhere I think they could be useful, but rather restrict myself to the code I write.

BowserJr wrote:Pretty much all the code was written by one person, so how he was taught will have a big influence.
I only fear that this person isn’t going to tell us his reasoning in the foreseeable future... :(

Vale,
Quintus
Come to our fork of SMC: https://secretchronicles.org
Quintus
SMC Team
SMC Team
 
Posts: 354
Joined: 14 Sep 2010 18:05

Re: Exceptions

Postby BowserJr » 07 May 2012 19:22

Really just that exceptions aren't so much built into the C++ standard as they are in many languages, and they're generally seen as opt-in via a compiler flag, and have an associated overhead. People from a C background, or teaching about keeping programs as simple as possible often won't include them, or will discourage their use. Because of the overhead, they may be seen as a feature to add later, or when exceptions are required by a third party library. Also, it's quite normal to follow the same exception handling pattern as the libraries you're using.These are all just guesses, but I know exceptions generally aren't as prevalent in C++ as in languages where they're part of the specification, such as Java.
The possibility of using them wrongly seems like a silly reason to avoid them, but most of my experience is in Java, so I'm pretty used to them as a standard feature.
"Plants need to have water poured on them because they have no hands to hold glasses of water."
User avatar
BowserJr
SMC Team
SMC Team
 
Posts: 1543
Joined: 05 Feb 2007 14:07
Location: London, UK

Re: Exceptions

Postby Quintus » 07 May 2012 20:46

BowserJr wrote:People from a C background, or teaching about keeping programs as simple as possible often won't include them, or will discourage their use.
Actually I think exceptions are a great way to make code easier. No bloated errorcode handling/propagating, clear error messages (what does this error code stand for?), more useful information (with exception classes you have an inspectable object that converys more than just a number or message), better error spotting thanks to stacktraces. I’ve programmed in C, and it’s a pity C has no exceptions (however not surprising given the age of the language). I’ve seen people using jumps for replacing exceptions there which appears to me as an awkward workaround around a missing language feature.
What’s true is that C++ exception handling misses something like finally or ensure which can sometimes lead to memory problems when not being careful. However when used with care these cases can be avoided for the most part.

BowserJr wrote:Because of the overhead, they may be seen as a feature to add later
Adding them later will cause a quite big amount of work compared with coding exception-friendly right from the beginning. Which is why I won’t rework the complete SMC code for adding exceptions.

BowserJr wrote:or when exceptions are required by a third party library.
SMC has this when dealing with CEGUI, which is a library that actually throws exceptions (which I appreciate).

BowserJr wrote:Also, it's quite normal to follow the same exception handling pattern as the libraries you're using.
You’re more experienced than me, hence I cannot object here. I’ve not worked on any big C++ project beside SMC now.

BowserJr wrote:most of my experience is in Java, so I'm pretty used to them as a standard feature.
I’m coming from Ruby, and exceptions are a standard feature there as well (even with ensure which is Ruby’s equivalent to Java’s finally). In fact I was pretty surprised to actually see people arguing against the use of exceptions when I was learning C++ (which is still kinda new to me, tough).

Vale,
Quintus
Come to our fork of SMC: https://secretchronicles.org
Quintus
SMC Team
SMC Team
 
Posts: 354
Joined: 14 Sep 2010 18:05

Re: Exceptions

Postby BowserJr » 08 May 2012 00:04

Yeah, I think CEGUI and Boost both use exceptions, but SDL doesn't, and originally the code was just for SDL, so didn't have that requirement. Maybe, I don't actually think I've been following SMC that long.

Also, game programming is even more performance critical than most code, so C++ exceptions may be less desirable: http://pkisensee.wordpress.com/2007/01/ ... uidelines/ and http://www.gamedev.net/page/resources/_ ... dling-r953 .

Again, not really sure if it was a coding style choice or a performance choice, but there's only a performance hit if you constantly throw and then catch a few layers up. Used sparingly, and only on an actual fail condition probably won't make a lot of difference. But I'm by no means an expert.... :?
"Plants need to have water poured on them because they have no hands to hold glasses of water."
User avatar
BowserJr
SMC Team
SMC Team
 
Posts: 1543
Joined: 05 Feb 2007 14:07
Location: London, UK

Re: Exceptions

Postby Quintus » 09 May 2012 22:40

BowserJr wrote:But I'm by no means an expert.... :?
I am neither. Thank you for the links, I’m going to read the articles in the next days...

BowserJr wrote:Used sparingly, and only on an actual fail condition probably won't make a lot of difference
Which is what I intend to do. However, I can understand your concerns... Hence I’m not going to add exceptions yet, but only add comments where I’d place exception code.

Vale,
Quintus
Come to our fork of SMC: https://secretchronicles.org
Quintus
SMC Team
SMC Team
 
Posts: 354
Joined: 14 Sep 2010 18:05

Re: Exceptions

Postby Luiji » 16 May 2012 19:59

I'd definitely recommend pro-exceptions for fatal errors. Warnings should be sent to the terminal and anything else shouldn't even consider exceptions (people have even used them to check for the end-of-file!). IMO SMC's code base is aging and some of the code could use rewriting if any of us had more free time.
Custom Built IBM-Compatible (Arch Linux w/ KDE)
Dell Vostro (Windows 7)
Dell Inspiron 1440 (Windows Server 2003, Debian w/ KDE)
Dell Inspiron 11z (Linux Mint)
Luiji
MVP
MVP
 
Posts: 2703
Joined: 14 Jan 2010 23:20
Location: The Mushroom Kyngdom

Re: Exceptions

Postby Quintus » 16 May 2012 20:24

Luiji wrote:I'd definitely recommend pro-exceptions for fatal errors.
I agree.

Luiji wrote:(people have even used them to check for the end-of-file!)
I’ve seen exceptions used for breaking out of loops. Nothing surprises me anymore.

Luiji wrote:IMO SMC's code base is aging and some of the code could use rewriting if any of us had more free time.


Hmm... Kickstarter? SMC is quite popular and I think we have a good bunch of ideas from the forums. But discussing that idea probably should go into another thread. And I honestly think that we should try to reach FluXy before going that way.

Vale,
Quinuts
Come to our fork of SMC: https://secretchronicles.org
Quintus
SMC Team
SMC Team
 
Posts: 354
Joined: 14 Sep 2010 18:05

Re: Exceptions

Postby BowserJr » 16 May 2012 23:01

Don't think I've seen Kickstarter being used for open source stuff before, and the reward system certainly doesn't apply nicely to a product which is ultimately free.
"Plants need to have water poured on them because they have no hands to hold glasses of water."
User avatar
BowserJr
SMC Team
SMC Team
 
Posts: 1543
Joined: 05 Feb 2007 14:07
Location: London, UK

Re: Exceptions

Postby Quintus » 19 May 2012 18:32

BowserJr wrote:Don't think I've seen Kickstarter being used for open source stuff before, and the reward system certainly doesn't apply nicely to a product which is ultimately free.

Sounds reasonable. However, just an idea.

Vale,
Quintus
Come to our fork of SMC: https://secretchronicles.org
Quintus
SMC Team
SMC Team
 
Posts: 354
Joined: 14 Sep 2010 18:05

Re: Exceptions

Postby FluXy » 28 Dec 2012 14:13

Yes, exceptions are okay.
Even boost overused exceptions sometimes :hihi:
If i didn't read your post but it was important you can send me a PM.
Any Donation will help this project!
User avatar
FluXy
Admin
Admin
 
Posts: 2858
Joined: 04 May 2004 19:44
Location: Germany


Return to Development

Who is online

Users browsing this forum: No registered users and 1 guest

cron