Skip to main content
gamedev.net
🔒 Locked

exceptions???

Started by ugor 3 replies 350+ views
Original Post
ugor
ugor
Let’s say that if a language like java didn’t have exceptions ie catching errors, what could be a possible solution so that error catching can be done? The reason why I am asking this is because I was thinking of making a method of reporting errors when I am programming in other languages that done have a feature like this, but I have no idea of how this might be done.
Sneftel
Sneftel
There''s a laundry list of methods. Here''s a few that spring to mind.

* All functions return an error code, either as a return value or into an argument via pass-by-reference. The calling functions are responsible for checking that error code.
* A user-defined function is called on errors.
* A longjmp is done to an error handler.
* A global variable is set to the error code.
* The application is ended.

All of these have drawbacks. What language(s) do you need this for?


"Sneftel is correct, if rather vulgar." --Flarelocke
ugor
ugor
I was thinking in general terms so that it could work on all languages. But if I was to implement it, I would do it in C.
Sneftel
Sneftel
In C the best method IMHO is setjmp/longjmp. It''s a bad idea to specify a method for "all" languages, since most languages have a specific "best" way to do it.


"Sneftel is correct, if rather vulgar." --Flarelocke
quorn
quorn
programming for exceptions and exception safety will change the design of your code.

in c++ the exceptional path of code is separated from the normal flow. try and catch actually isn''t what it''s about. it''s more to do with leaving things in a valid state if an exception occurs. read up on RAII (resource acquisition is initialisation).

relying upon return variables or having to check some global variable will affect the normal flow of code and result in exception safety being mixed up with everything else.

if you wrap c++ in boost.python you can translate between c++ and python exceptions. they are a great way to keep normal program flow clean.

read up on gotw.ca about everything to do with writing exception safe code.

eg.
Try and Catch Me
Difficulty: 3 / 10

Is exception safety all about writing try and catch in the right places? If not, then what? And what kinds of things should you consider when developing an exception safety policy for your software?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.