www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - global exceptions handler

reply "Asaf Karagila" <kas1 netvision.net.il> writes:
is there a way to install a global exception handler,
that will handle any unexpected error that occurs, that has no specific 
handler in the code..
like, a default exception handler..

- Asaf 
Oct 19 2004
next sibling parent reply Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
Asaf Karagila wrote:
 is there a way to install a global exception handler,
 that will handle any unexpected error that occurs, that has no specific 
 handler in the code..
 like, a default exception handler..

You can put one in main(). That will catch everything except for exceptions which occur before main() runs (that is, exceptions in module initializers and such) or exceptions which occur after main() completes (module destructors and such).
Oct 19 2004
parent reply "Martin M. Pedersen" <martin moeller-pedersen.dk> writes:
"Russ Lewis" <spamhole-2001-07-16 deming-os.org> skrev i en meddelelse
news:cl3t0h$oqb$1 digitaldaemon.com...
 Asaf Karagila wrote:
 is there a way to install a global exception handler,


That will work for the main thread only. Regards, Martin
Oct 19 2004
parent reply "Asaf Karagila" <kas1 netvision.net.il> writes:
well, its a simple code, one thread only..
so there is no problem for that.
but how do i trap everything ?

- Asaf

"Martin M. Pedersen" <martin moeller-pedersen.dk> wrote in message 
news:cl4cc6$1890$1 digitaldaemon.com...
 "Russ Lewis" <spamhole-2001-07-16 deming-os.org> skrev i en meddelelse
 news:cl3t0h$oqb$1 digitaldaemon.com...
 Asaf Karagila wrote:
 is there a way to install a global exception handler,


That will work for the main thread only. Regards, Martin

Oct 19 2004
parent reply Regan Heath <regan netwin.co.nz> writes:
On Wed, 20 Oct 2004 06:07:34 +0200, Asaf Karagila <kas1 netvision.net.il> 
wrote:
 well, its a simple code, one thread only..
 so there is no problem for that.
 but how do i trap everything ?

Try.. import std.asserterror; int main(char[][] args) { try { throw new Exception("aaa"); assert(false); } catch(Object o) { AssertError a = cast(AssertError)o; if (a !== null) { printf("ASSERT: %.*s\n",a.toString()); return 1; } Exception e = cast(Exception)o; if (e !== null) { printf("Exception: %.*s\n",e.toString()); return 1; } printf("Unknown: %.*s\n",o.toString()); return 1; } return 0; } Regan -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Oct 20 2004
parent reply "Asaf Karagila" <kas1 netvision.net.il> writes:
You forgot the finally{
as well, i couldn't really understand. because this example is using 
asserterror.
if i want to catch both Base64 exceptions, and as well file operations 
exceptions..
and if any other exception occurs, to catch it as well..
how can i do that ?
within one try-catch-finally block..

- Asaf

"Regan Heath" <regan netwin.co.nz> wrote in message 
news:opsf6r38g85a2sq9 digitalmars.com...
 On Wed, 20 Oct 2004 06:07:34 +0200, Asaf Karagila <kas1 netvision.net.il> 
 wrote:
 well, its a simple code, one thread only..
 so there is no problem for that.
 but how do i trap everything ?

Try.. import std.asserterror; int main(char[][] args) { try { throw new Exception("aaa"); assert(false); } catch(Object o) { AssertError a = cast(AssertError)o; if (a !== null) { printf("ASSERT: %.*s\n",a.toString()); return 1; }

 Exception e = cast(Exception)o;
 if (e !== null) {
 printf("Exception: %.*s\n",e.toString());
 return 1;
 }

 printf("Unknown: %.*s\n",o.toString()); return 1;
 }
 return 0;
 }

 Regan

 -- 
 Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/ 

Oct 21 2004
parent reply Regan Heath <regan netwin.co.nz> writes:
On Fri, 22 Oct 2004 00:56:43 +0200, Asaf Karagila <kas1 netvision.net.il> 
wrote:

 You forgot the finally{

Good point.
 as well, i couldn't really understand. because this example is using
 asserterror.

Actually it's using 'Object', it will catch everything. It then goes on to cast to AssertError as that is a specific type of Object which can be thrown/caught.
 if i want to catch both Base64 exceptions, and as well file operations
 exceptions..
 and if any other exception occurs, to catch it as well..
 how can i do that ?

What I have below will catch all of them.
 within one try-catch-finally block..

int main(char[][] args) { try { } catch(Object o) { } finally { } } Regan
 - Asaf

 "Regan Heath" <regan netwin.co.nz> wrote in message
 news:opsf6r38g85a2sq9 digitalmars.com...
 On Wed, 20 Oct 2004 06:07:34 +0200, Asaf Karagila 
 <kas1 netvision.net.il>
 wrote:
 well, its a simple code, one thread only..
 so there is no problem for that.
 but how do i trap everything ?

Try.. import std.asserterror; int main(char[][] args) { try { throw new Exception("aaa"); assert(false); } catch(Object o) { AssertError a = cast(AssertError)o; if (a !== null) { printf("ASSERT: %.*s\n",a.toString()); return 1; }

 Exception e = cast(Exception)o;
 if (e !== null) {
 printf("Exception: %.*s\n",e.toString());
 return 1;
 }

 printf("Unknown: %.*s\n",o.toString()); return 1;
 }
 return 0;
 }

 Regan

 --
 Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/


-- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Oct 21 2004
parent reply "Asaf Karagila" <kas1 netvision.net.il> writes:
ok, i think i got the idea of using Object to catch everything...
but how can i tell which exception is needed to be casted ?
because a Base64 exception won't fit into a File exception..

- Asaf

"Regan Heath" <regan netwin.co.nz> wrote in message 
news:opsf8v9z0b5a2sq9 digitalmars.com...
 On Fri, 22 Oct 2004 00:56:43 +0200, Asaf Karagila <kas1 netvision.net.il> 
 wrote:

 You forgot the finally{

Good point.
 as well, i couldn't really understand. because this example is using
 asserterror.

Actually it's using 'Object', it will catch everything. It then goes on to cast to AssertError as that is a specific type of Object which can be thrown/caught.
 if i want to catch both Base64 exceptions, and as well file operations
 exceptions..
 and if any other exception occurs, to catch it as well..
 how can i do that ?

What I have below will catch all of them.
 within one try-catch-finally block..

int main(char[][] args) { try { } catch(Object o) { } finally { } } Regan
 - Asaf

 "Regan Heath" <regan netwin.co.nz> wrote in message
 news:opsf6r38g85a2sq9 digitalmars.com...
 On Wed, 20 Oct 2004 06:07:34 +0200, Asaf Karagila 
 <kas1 netvision.net.il>
 wrote:
 well, its a simple code, one thread only..
 so there is no problem for that.
 but how do i trap everything ?

Try.. import std.asserterror; int main(char[][] args) { try { throw new Exception("aaa"); assert(false); } catch(Object o) { AssertError a = cast(AssertError)o; if (a !== null) { printf("ASSERT: %.*s\n",a.toString()); return 1; }

 Exception e = cast(Exception)o;
 if (e !== null) {
 printf("Exception: %.*s\n",e.toString());
 return 1;
 }

 printf("Unknown: %.*s\n",o.toString()); return 1;
 }
 return 0;
 }

 Regan

 --
 Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/


-- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

Oct 22 2004
parent reply Regan Heath <regan netwin.co.nz> writes:
On Fri, 22 Oct 2004 09:10:19 +0200, Asaf Karagila <kas1 netvision.net.il> 
wrote:

 ok, i think i got the idea of using Object to catch everything...
 but how can i tell which exception is needed to be casted ?
 because a Base64 exception won't fit into a File exception..

As far as I know, you can't tell. When you cast, if you cast to something that is incorrect (not the same class, or parent of) you will get null. In general you should catch and handle exceptions where they occur, this sort of catch all is really only useful if your program _must_ perform some task before halting. Regan -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Oct 25 2004
parent reply "Asaf Karagila" <kas1 netvision.net.il> writes:
well, i followed your advice,
i've put a selective exception handler,
look at the following code:
         char[] data;
         try{
     data=cast(char[])std.file.read(d(filename));
            }
         catch(Base64Exception ex) {
              say(d(badfile));
              say("\n" ~ d(goodbye));
              std.c.stdio.getch();
              return 0;
              }
         finally{};

d() is an alias for base64.decode()
say is an alias for writef()

i declared data outside of the try-catch block because otherwise i couldn't 
have use the .length property of the array,
anyway..
it does compile, but when i run it an use a test file that contain the 
illegal base64 char "!"
i get this:
           Error: Invalid base64 character '!'

so why doesn't the exception is being catched ?
i tried just as well with Base64CharException, gives the same result.

- Asaf 
Oct 26 2004
parent reply Regan Heath <regan netwin.co.nz> writes:
Hi,

On Tue, 26 Oct 2004 21:47:07 +0200, Asaf Karagila <kas1 netvision.net.il> 
wrote:
 well, i followed your advice,
 i've put a selective exception handler,
 look at the following code:
          char[] data;
          try{
      data=cast(char[])std.file.read(d(filename));
             }
          catch(Base64Exception ex) {
               say(d(badfile));                               say("\n" ~ 
 d(goodbye));
               std.c.stdio.getch();
               return 0;
               }
          finally{};

 d() is an alias for base64.decode()
 say is an alias for writef()

 i declared data outside of the try-catch block because otherwise i 
 couldn't
 have use the .length property of the array,
 anyway..
 it does compile, but when i run it an use a test file that contain the
 illegal base64 char "!"
 i get this:
            Error: Invalid base64 character '!'

 so why doesn't the exception is being catched ?
 i tried just as well with Base64CharException, gives the same result.

The exception you want is 'Base64CharException' if you look in std.base64 you'll find it being thrown. The problem with the code above is this line: say(d(badfile)); it is also giving the exception, so while your code catches the exception thrown by data=cast(char[])std.file.read(d(filename)); it then generates another exception on say(d(badfile)); and prints it's own error. You can verify this by inserting 'say' calls eg. import std.base64; import std.stdio; import std.file; alias std.base64.decode d; alias std.stdio.writef say; int main(char[][] args) { char[] filename = "one!.txt"; char[] badfile = "two!.txt"; char[] goodbye = "bye!"; char[] data; try{ say("a\n"); data = cast(char[])std.file.read(d(filename)); say("b\n"); } catch(Base64CharException e) { say("c\n"); say(d(badfile)); say("d\n"); say("\n" ~ d(goodbye)); std.c.stdio.getch(); return 0; } finally{}; } Which outputs: a c Error: Invalid base64 character '!' Regan -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Oct 26 2004
parent "Asaf Karagila" <kas1 netvision.net.il> writes:
i forgot to mention that, my bad.
filename, badfile, goodbye, alld the strings are already encoded in base64,
so its not really the problem of the decoding,
i was sticking the try-catch at the wrong location, i should've stuck it 
when it decoded the data.
and now when i did, it worked great.
thanks a lot for all the help !

- Asaf.

"Regan Heath" <regan netwin.co.nz> wrote in message 
news:opsghu7jwe5a2sq9 digitalmars.com...
 Hi,

 On Tue, 26 Oct 2004 21:47:07 +0200, Asaf Karagila <kas1 netvision.net.il> 
 wrote:
 well, i followed your advice,
 i've put a selective exception handler,
 look at the following code:
          char[] data;
          try{
      data=cast(char[])std.file.read(d(filename));
             }
          catch(Base64Exception ex) {
               say(d(badfile));                               say("\n" ~ 
 d(goodbye));
               std.c.stdio.getch();
               return 0;
               }
          finally{};

 d() is an alias for base64.decode()
 say is an alias for writef()

 i declared data outside of the try-catch block because otherwise i 
 couldn't
 have use the .length property of the array,
 anyway..
 it does compile, but when i run it an use a test file that contain the
 illegal base64 char "!"
 i get this:
            Error: Invalid base64 character '!'

 so why doesn't the exception is being catched ?
 i tried just as well with Base64CharException, gives the same result.

The exception you want is 'Base64CharException' if you look in std.base64 you'll find it being thrown. The problem with the code above is this line: say(d(badfile)); it is also giving the exception, so while your code catches the exception thrown by data=cast(char[])std.file.read(d(filename)); it then generates another exception on say(d(badfile)); and prints it's own error. You can verify this by inserting 'say' calls eg. import std.base64; import std.stdio; import std.file; alias std.base64.decode d; alias std.stdio.writef say; int main(char[][] args) { char[] filename = "one!.txt"; char[] badfile = "two!.txt"; char[] goodbye = "bye!"; char[] data; try{ say("a\n"); data = cast(char[])std.file.read(d(filename)); say("b\n"); } catch(Base64CharException e) { say("c\n"); say(d(badfile)); say("d\n"); say("\n" ~ d(goodbye)); std.c.stdio.getch(); return 0; } finally{}; } Which outputs: a c Error: Invalid base64 character '!' Regan -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

Oct 26 2004
prev sibling next sibling parent "Matthew" <admin.hat stlsoft.dot.org> writes:
Not yet, but that's one of the things that will be suggested shortly, in a
forthcoming review of the exceptions 
infrastructure

"Asaf Karagila" <kas1 netvision.net.il> wrote in message
news:cl3o0e$j6i$1 digitaldaemon.com...
 is there a way to install a global exception handler,
 that will handle any unexpected error that occurs, that has no specific
handler in the code..
 like, a default exception handler..

 - Asaf
 

Oct 19 2004
prev sibling parent reply Sai <Sai_member pathlink.com> writes:
What about putting a try-catch block in main ?


In article <cl3o0e$j6i$1 digitaldaemon.com>, Asaf Karagila says...
is there a way to install a global exception handler,
that will handle any unexpected error that occurs, that has no specific 
handler in the code..
like, a default exception handler..

- Asaf 

Oct 27 2004
parent reply "Asaf Karagila" <kas1 netvision.net.il> writes:
ok, and what exactly is it suppose to cacth,
and how would it identify between one exception to another ?

- Asaf

"Sai" <Sai_member pathlink.com> wrote in message 
news:clptdo$15oh$1 digitaldaemon.com...
 What about putting a try-catch block in main ?


 In article <cl3o0e$j6i$1 digitaldaemon.com>, Asaf Karagila says...
is there a way to install a global exception handler,
that will handle any unexpected error that occurs, that has no specific
handler in the code..
like, a default exception handler..

- Asaf


Oct 28 2004
parent reply Regan Heath <regan netwin.co.nz> writes:
On Thu, 28 Oct 2004 17:43:58 +0200, Asaf Karagila <kas1 netvision.net.il> 
wrote:
 ok, and what exactly is it suppose to cacth,

Object will catch everything.
 and how would it identify between one exception to another ?

Using cast. Cast returns null if you try to cast to the wrong thing, so you can use a series of casts to determine the exception. Regan
 - Asaf

 "Sai" <Sai_member pathlink.com> wrote in message
 news:clptdo$15oh$1 digitaldaemon.com...
 What about putting a try-catch block in main ?


 In article <cl3o0e$j6i$1 digitaldaemon.com>, Asaf Karagila says...
 is there a way to install a global exception handler,
 that will handle any unexpected error that occurs, that has no specific
 handler in the code..
 like, a default exception handler..

 - Asaf



-- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Oct 28 2004
parent reply "Asaf Karagila" <kas1 netvision.net.il> writes:
yeah, i understood that earlier from your posts,
but what if its an unexpected exception ?
the only way i can think of now is to install the handler using
the winapi SetUnhandledExceptionFilter, but i sorta hoped there would be
something like that built-in the language..

- Asaf.

"Regan Heath" <regan netwin.co.nz> wrote in message 
news:opsglknzq75a2sq9 digitalmars.com...
 On Thu, 28 Oct 2004 17:43:58 +0200, Asaf Karagila <kas1 netvision.net.il> 
 wrote:
 ok, and what exactly is it suppose to cacth,

Object will catch everything.
 and how would it identify between one exception to another ?

Using cast. Cast returns null if you try to cast to the wrong thing, so you can use a series of casts to determine the exception. Regan
 - Asaf

 "Sai" <Sai_member pathlink.com> wrote in message
 news:clptdo$15oh$1 digitaldaemon.com...
 What about putting a try-catch block in main ?


 In article <cl3o0e$j6i$1 digitaldaemon.com>, Asaf Karagila says...
 is there a way to install a global exception handler,
 that will handle any unexpected error that occurs, that has no specific
 handler in the code..
 like, a default exception handler..

 - Asaf



-- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

Oct 29 2004
next sibling parent Vathix <vathix dprogramming.com> writes:
On Fri, 29 Oct 2004 12:25:54 +0200, Asaf Karagila <kas1 netvision.net.il>  
wrote:

 yeah, i understood that earlier from your posts,
 but what if its an unexpected exception ?
 the only way i can think of now is to install the handler using
 the winapi SetUnhandledExceptionFilter, but i sorta hoped there would be
 something like that built-in the language..

 - Asaf.

catch{} or catch(Object o){} will catch ALL exceptions. Even the Win32 exceptions are converted internally by D. Try something like this: try { // stuff *(cast(int*)0) = 0; // test } catch(ExpectedException e) { // something } catch(SomeOtherExpectedException e) { // something else } catch(Object e) { writef("Unexpected exception: %s\n", e.toString()); }
Oct 29 2004
prev sibling parent reply larrycowan <larrycowan_member pathlink.com> writes:
In article <clt5q6$27f2$1 digitaldaemon.com>, Asaf Karagila says...
yeah, i understood that earlier from your posts,
but what if its an unexpected exception ?
the only way i can think of now is to install the handler using
the winapi SetUnhandledExceptionFilter, but i sorta hoped there would be
something like that built-in the language..

- Asaf.

What do you expect to be able to do with an "unexpected exception"? Cancel with message or ignore and continue are the only things you could count on doing, and the ignore is probably difficult from a single catch point and the wrong thing to do. If this is for testing, there are better ways. If for production, why would you want to do anything but kill the process and fix the problem? Log it and continue in some totally "unexpected, and wrong" state doing things wrong endlessly? If there are known possibilities you are taking into account and have a plan to handle them, that's one thing and should be handled near the point it occurs, but global "unexpected" is either what it says or programmer laziness and/or error hiding. The catch Object will trap everything after main() starts, and if it dies before that you don't have anything you're able to do except fix and restart anyway. Possibly you could have something in static initializations that would depend on system state at startup, but I can't think of an example. If you have a specific example of why you need this we all would be interested I'm sure. Are you trying to do all your trapping of errors at global (main()) level, and just spit out a message about what kind it was, maybe continue to find more? That sounds like debugging, and you would have to go in and trap closer to the error anyway if you have no other way to isolate it. D compiler doesn't give you a whole pile of perhaps interrelated error messages - that's old batch compile mode - D presumes a fairly interactive mode of programming. Big lists of errors usually don't lead to the next compile being clean and correct anyway. You should probably treat your testing similarly. Test, find, fix, test, find, fix, ...
Oct 29 2004
parent "Asaf Karagila" <kas1 netvision.net.il> writes:
well,
i think i'll let out my "little secret",
i'm a reverser. when i learn a new language,
i usually try to write a crackme, or a reverseme,
its a bit more complicated than the simple hello world programs,
and it can contain many features i need to know for the future.

i wrote a crackme that reads a keyfile and test it with md5 and base64,
what i forgot is to handle the case of the file containing plain data,
which is illegal base64 characters. i catched it pretty well eventually.
but i thought about using a global exception handler to catch ANY error,
and apply it to the fact of something wrong with the registration process.
or to use it as an anti-debugging technique just as well..

i think i know how to do it now. but i'm still not certain about it.
i tried couple of exceptions,
int 3;
int 1;
writing into the code section;
reading from [0000000] (it didn't go with mov EAX, dword [00000000]; a bug ? 
i used xor ESI, ESI; lodsd;)

except the int3 which gave me the output Win32 Exception,
all the rest gave me Access Violation..
what i need to know is how to get the data about the exception.. where it 
occured ?
then its possible to have it analyzing on run, and fixed by some self 
modifying code..

- Asaf.

"larrycowan" <larrycowan_member pathlink.com> wrote in message 
news:cluu3j$p11$1 digitaldaemon.com...
 In article <clt5q6$27f2$1 digitaldaemon.com>, Asaf Karagila says...
yeah, i understood that earlier from your posts,
but what if its an unexpected exception ?
the only way i can think of now is to install the handler using
the winapi SetUnhandledExceptionFilter, but i sorta hoped there would be
something like that built-in the language..

- Asaf.

What do you expect to be able to do with an "unexpected exception"? Cancel with message or ignore and continue are the only things you could count on doing, and the ignore is probably difficult from a single catch point and the wrong thing to do. If this is for testing, there are better ways. If for production, why would you want to do anything but kill the process and fix the problem? Log it and continue in some totally "unexpected, and wrong" state doing things wrong endlessly? If there are known possibilities you are taking into account and have a plan to handle them, that's one thing and should be handled near the point it occurs, but global "unexpected" is either what it says or programmer laziness and/or error hiding. The catch Object will trap everything after main() starts, and if it dies before that you don't have anything you're able to do except fix and restart anyway. Possibly you could have something in static initializations that would depend on system state at startup, but I can't think of an example. If you have a specific example of why you need this we all would be interested I'm sure. Are you trying to do all your trapping of errors at global (main()) level, and just spit out a message about what kind it was, maybe continue to find more? That sounds like debugging, and you would have to go in and trap closer to the error anyway if you have no other way to isolate it. D compiler doesn't give you a whole pile of perhaps interrelated error messages - that's old batch compile mode - D presumes a fairly interactive mode of programming. Big lists of errors usually don't lead to the next compile being clean and correct anyway. You should probably treat your testing similarly. Test, find, fix, test, find, fix, ...

Oct 30 2004