Nice lil' code snippet

Started by
2 comments, last by JmarsKoder 22 years, 10 months ago
Thanks to ashe, he showed me tim sweeny's (creator of unreal and ut engine) method of exception hanldling. which looks like:


#define guard(s) static char * __FUNCTION_NAME__ = #s; \ 
                 try { 
#define unguard  } catch (...) { \ 
                 Log( "Exception in: %s\n", __FUNCTION_NAME__ ); \ 
                 throw } 

Here is my modification, to include some error handling too.


//Exception Handler--------------------------------------------------------
#define guard(s) static char * __FUNCTION_NAME__ = #s;                  try { 
#define unguard  }                  catch (char *cs)                  {                    string st=cs; st+="\nException in: ";                    st+=__FUNCTION_NAME__; st+='\n';                    throw st;                  }                  catch (string str){throw str;}                  catch (...) {                  string cs="Exception in: ";                  cs+=__FUNCTION_NAME__; cs+='\n';                  throw cs; }
//-------------------------------------------------------------------------

Just thought it might benifit you. Reality Makes Me XIC I don't do spelling, I hack code: passion is my feul. Use my programs, experience genius. http://www.x-i-c.com/ Edited by - JmarsKoder on June 28, 2001 11:17:59 AM
I am XiCI don't do talk, I code: passion is my feul. Use my programs, experience XiC. http://www.x-i-c.com/
Advertisement
Sorry about that, here are my real modifications


//Exception Handler--------------------------------------------------------#define guard(s) static char * __FUNCTION_NAME__ = #s; \                 try { #define unguard  } \                 catch (char *cs) \                 { \                   string st=cs; st+="\nException in: "; \                   st+=__FUNCTION_NAME__; st+=''\n''; \                   throw st; \                 } \                 catch (string str){throw str;} \                 catch (...) { \                 string cs="Exception in: "; \                 cs+=__FUNCTION_NAME__; cs+=''\n''; \                 throw cs; }//-------------------------------------------------------------------------



Reality Makes Me XIC
I don''t do spelling, I hack code: passion is my feul. Use my programs, experience genius.
http://www.x-i-c.com/
I am XiCI don't do talk, I code: passion is my feul. Use my programs, experience XiC. http://www.x-i-c.com/
Here is the usage of the exception handler.

//Format: (#,#,#) (#,#,#) (#,#,#)void plane::read_ascii(basic_scanner &s){  guard(plane::read_ascii(basic_scanner &s));  clear();  for(uint i=0; i<3; i++)  {    s.get_token();    if(s.token()!="(")      {throw "Invalid Plane Point!\n";}    v.read_ascii(s);    s.get_token();    if(s.token()!=")")      {throw "Invalid Plane Point!\n";}  }  calc();  unguard;}


Reality Makes Me XIC
I don''t do spelling, I hack code: passion is my feul. Use my programs, experience genius.
http://www.x-i-c.com/
I am XiCI don't do talk, I code: passion is my feul. Use my programs, experience XiC. http://www.x-i-c.com/
The only problem I see with your modifications is that you end
up allocating and modifing string pointers during an exception. This will work, but could lead to a cascade exception from an
out of memory error. The original version used static string and complied strings without any dynamic allocation. I even
have issue in the first version in that the string "Exception in
%s:" will possibly be added to every function, there is no reason that could not be added as a global or local const so the
compiler would optimize it down to a pointer.

Hope that helps


Edited by - snowmoon on June 28, 2001 11:52:44 AM

Edited by - snowmoon on June 28, 2001 11:53:11 AM

This topic is closed to new replies.

Advertisement