Asserts vs Try/Catch Exceptions

BlitzMax Forums/BlitzMax Programming/Asserts vs Try/Catch Exceptions

Macguffin(Posted 2008) [#1]
Hey all,

Trying to get my head around asserts vs exceptions. I read a number of threads and articles, and I'm starting to get there. One big question I have right now, though, and I figured you were the crew to throw it at. :D

Asserts, from what I understand, are debug only - correct me on that if I'm wrong. So, in a release situation, I need to use try/catch for exceptions.

Say I've got data files that the user can edit, and I want to have things bomb out if their data is bad. Frex, they can pass in a unit spec, and -1 is an invalid value for attackPower. How would I want to structure a Try block to deal with that? Would I just throw an unhandled exception?

I'm sure there are more elegant ways to deal with something like this, but I'm still trying to get my head around the inelegant way. ;) Thanks.

(Edit - Playing around with it some more, I could catch the exception, but I guess I just need to figure out the system commands that would bring everything to a halt and send the message in a nice fashion.)


Macguffin(Posted 2008) [#2]
Yeah... I have a new game I play with myself. It's called "Timed RTFM". I see if I can ask a question on the forums, then figure out the answer myself before anyone can get back to me.

I'm guessing RuntimeError() is what I'm looking for here.


Czar Flavius(Posted 2008) [#3]
RunTimeError() seems a bit drastic for the kind of error you are talking about. It bombs the whole program for something that really isn't a fatal error. A bunch of If statements checking that the values you are loading are sensible, and if not, informing the user the data contains errors and asking them to make another choice, seems to be more appropriate.


Grey Alien(Posted 2008) [#4]
Yeah I'd use if statements. I wrap my whole game in one bit Try Except (it's in my framework code) and then if it bombs unexpectly, at least it's handled. However you only get a decent error message if it's a Debug build.


tonyg(Posted 2008) [#5]
Maybe Notify()?


Macguffin(Posted 2008) [#6]
Ok - thanks much, guys. I'll keep playing with this as I go. Looks like Notify might be good for the non-serious errors.