Skip to main content
gamedev.net
🔒 Locked

deciphering between exception types

Started by ZedFx Sep 27, 2005 at 7:11 PM 5 replies 1k views
Original Post
ZedFx
ZedFx
Take the following straight forward code:

char * a = (char *)53;

try
{
	a[3] = 97;

}

catch (...)
{
	// your handler

}

this will catch the error which occours (Access violation when writing to location), however, how can you determin which error you have caught. For example, could another error possibly be thrown, or would it always be that one in particular. Additionally, if another error can be thrown how do you find out what it is? I am currently using Win32 & VC.NET 2002, but will this sort of error be throw in other OS's, ie. Is this portable? Regards
SiCrane
SiCrane
It's not portable, it's an artifact of the implementation of the exception handling used by MSVC. (Although, it's overall a good thing, IMO.) In any case, by using (...) as your exception handling you by definition don't know what's going on. However, in this case you can use GetExceptionCode() from structured exception handling routines in the exception handling block of an exception handler. Very non-portable.
ZedFx
ZedFx
Ok, so GetExceptionCode would be able to tell you what has occourred in Win32. Now in other OS's would catch(...) catch exceptions generated in this sort of way or would they be handled by the OS ?
SiCrane
SiCrane
Depends on the operating system and the compiler.
Sneftel
Sneftel
Quote:
Original post by ZedFx
Ok, so GetExceptionCode would be able to tell you what has occourred in Win32. Now in other OS's would catch(...) catch exceptions generated in this sort of way or would they be handled by the OS ?

Runtimes aren't required to handle such behavior gracefully (by throwing an exception). So segfaults might well cause the OS to kill the program. Hell, under a non-protected mode OS, it might just bring the system down.
ZedFx
ZedFx
okey, thanks for the input, I just saw some code which enclosed a memcpy inside a try block and was curious.
Gluber
Gluber
Take a look at boost::execution_monitor
( part of boost www.boost.org if you don't know )
It handles those things very nicely, allowing you to get meaningful errors from any exception ( C++,SEH,Signals on Unix )
This should help you achieve exactly what you want ( e.g poping up an error box: "Access violation" or "Division by zero".

your code becomes:

int MyFunction()
{
// Any code that throws an exception
}

try
{
boost::execution::montior monitor;
monitor.execute( MyFunction )
}
// The following is conceptually the same as catch( ... ) it catches ANY
// exception but provides information about what happened.
catch( boost::exceution_exception & ex )
{
Output( ex.what() );
}

Topic Locked

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

Sign in to reply to this topic.