Skip to main content
GameDev.net gamedev.net
🔒 Locked

So an error ocurred...

Started by Prozak Oct 26, 2003 at 7:28 PM 6 replies 400+ views
Original Post
Prozak
Prozak
Ok, so an error occurred... If i threw it, i know (in principle) how to handle it... Now, for errors that I''m not expecting, like a divide by zero... How to i get info on where (mem location) that unexpected error occurred? And what type of error it might be...? Something like: catch(...) { MBox("Error occured in mem pos: "+error.mempos); MBox("Error is of type: "+error.type); } Even if you''re catching all the errors, there should be some structure that tells me, or gives me some sort of basic info on what just the f**k happened. Also, btw, how to say "I handled the error, return to normal program flow"? Thanx for the tips good ppl of GD.Net!

[Hugo Ferreira][Positronic Dreams][Colibri 3D Engine]
All your code are belong to us!

LessBread
LessBread
I don''t think it can be done cross platform. But in general, an unexpected exception indicates a fatal exception for the program.

On Windows you can set a crash handler using the SetUnhandledExceptionFilter API. The handler receives a pointer to an EXCEPTION_POINTERS structure, which in turn has members pointing to a pointer to an EXCEPTION_RECORD structure and a pointer to a CONTEXT structure. A CONTEXT structure contains the current values for the CPU registers. EIP holds the address of the faulting instruction, ESP holds the stack pointer and so forth.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
LessBread
LessBread
Here are a few google search terms that you might find helpful: dbghelp.dll, imagehlp.dll, CONTEXT, CONTEXT_INTEGER, unhandled exception, release mode debugging, SymInitialize, SymFunctionTableAccess, EXCEPTION_ILLEGAL_INSTRUCTION, Matt Pietrek, John Robbins.

It''s not too difficult to find unhandled exception handler code examples.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Prozak
Prozak
Thanx!

What about returning to the throw location?
I mean, if I throw an error that isnt that important for the flow of things, can the handler send control back to the place where the error occurred, and resume the program?
LessBread
LessBread
Yes, but...

If the unhandled exception filter function returns EXCEPTION_CONTINUE_EXECUTION, the program resumes at the faulting location. However, if the fault isn''t repaired, that could lead to an infinite recursion.

For breakpoints, debuggers advance the Eip member of the CONTEXT structure one byte and resume program execution there. However, debuggers typically use the SetThreadContext API to do that and the docs say that function requires that the target thread must be suspended. That presents a problem with an unhandled exception filter, because the unhandled exception filter itself runs in the faulting thread.

In the end I think it''s easier to treat unhandled exceptions as unrecoverable and use the generated crash report to locate bugs and fix them or if that''s not possible then to turn the unexpected exception into an expected exception.

"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Prozak
Prozak
Attention: Huge Necro...

quote:
For breakpoints, debuggers advance the Eip member of the CONTEXT structure one byte and resume program execution there.


how can it advance one byte? not all instruction operands are one-byte sized, you could be jumping back to code that really screws things up...?!


[Hugo Ferreira][Positronic Dreams][Colibri 3D Engine][Entropy HL2 MOD][Yann L.][Enginuity]
The most irrefutable evidence that there is intelligent life in the Universe is that they haven''t contacted us!

Topic Locked

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

Sign in to reply to this topic.