Thread overview | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
October 19, 2004 global exceptions handler | ||||
---|---|---|---|---|
| ||||
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 |
October 19, 2004 Re: global exceptions handler | ||||
---|---|---|---|---|
| ||||
Posted in reply to Asaf Karagila | 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).
|
October 19, 2004 Re: global exceptions handler | ||||
---|---|---|---|---|
| ||||
Posted in reply to Asaf Karagila | 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 > |
October 20, 2004 Re: global exceptions handler | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russ Lewis | "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, > You can put one in main(). That will work for the main thread only. Regards, Martin |
October 20, 2004 Re: global exceptions handler | ||||
---|---|---|---|---|
| ||||
Posted in reply to Martin M. Pedersen | 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, >> You can put one in main(). > > That will work for the main thread only. > > Regards, > Martin > > |
October 20, 2004 Re: global exceptions handler | ||||
---|---|---|---|---|
| ||||
Posted in reply to Asaf Karagila | 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/ |
October 21, 2004 Re: global exceptions handler | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | 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; > } finally { > 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/ |
October 22, 2004 Re: global exceptions handler | ||||
---|---|---|---|---|
| ||||
Posted in reply to Asaf Karagila | 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; >> } > finally { >> 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/ |
October 22, 2004 Re: global exceptions handler | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | 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; >>> } >> finally { >>> 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/ |
October 25, 2004 Re: global exceptions handler | ||||
---|---|---|---|---|
| ||||
Posted in reply to Asaf Karagila | 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/ |
Copyright © 1999-2021 by the D Language Foundation