www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Error and Exception Handling

reply "Asaf Karagila" <kas1 netvision.net.il> writes:
well, this is some-sort of a request for more examples of using 
try-catch-finally blocks,
because there is only one simple example on JCC's site and i would like to 
see some more code
using it, so if possible to have couple more examples about it,
basicly my problem is that i have a pretty big block to catch all kind of 
errors on, so i would like to know
how to catch several errors (or just catch them all) in a single/multiple 
catch()..

Cheers,
Asaf 
Oct 11 2004
next sibling parent reply teqDruid <me teqdruid.com> writes:
try
{
	//stuff
}
catch (SomeException e)
{
	//Just make all of the exceptions extend SomeException
}
catch (Object o)
{
	//Runs is anything other than SomeException or it's children are thrown
}
finally
{
	//Always runs
}

It's just like Java... Unless I've been doing something horribly wrong. 
If you can't make all of the exceptions you want to catch children of one
exception, then you'll have to have each catch call a function, ie:
try
{
	//FOOBAR!!!
}
catch (FooException e)
{
	fuBar(e);
}
catch (BarException f)
{
	fuBar(e);
}

void fuBar(Exception e)
{
}

The problem with something like the following:
catch (FooException e, BarException f)//Don't compile
is that either e or f would have to be null... so one may as well have
separate catches.  If you don't need an instance of the Exception, it
could work, however D don't support it.

John

On Mon, 11 Oct 2004 21:27:27 +0200, Asaf Karagila wrote:

 well, this is some-sort of a request for more examples of using 
 try-catch-finally blocks,
 because there is only one simple example on JCC's site and i would like to 
 see some more code
 using it, so if possible to have couple more examples about it,
 basicly my problem is that i have a pretty big block to catch all kind of 
 errors on, so i would like to know
 how to catch several errors (or just catch them all) in a single/multiple 
 catch()..
 
 Cheers,
 Asaf
Oct 11 2004
parent reply "Asaf Karagila" <kas1 netvision.net.il> writes:
"teqDruid" <me teqdruid.com> wrote in message 
news:pan.2004.10.12.04.49.46.185077 teqdruid.com...
 try
 {
 //stuff
 }
 catch (SomeException e)
 {
 //Just make all of the exceptions extend SomeException
 }
 catch (Object o)
 {
 //Runs is anything other than SomeException or it's children are thrown
 }
 finally
 {
 //Always runs
 }

 It's just like Java... Unless I've been doing something horribly wrong.
 If you can't make all of the exceptions you want to catch children of one
 exception, then you'll have to have each catch call a function, ie:
 try
 {
 //FOOBAR!!!
 }
 catch (FooException e)
 {
 fuBar(e);
 }
 catch (BarException f)
 {
 fuBar(e);
 }

 void fuBar(Exception e)
 {
 }

 The problem with something like the following:
 catch (FooException e, BarException f)//Don't compile
 is that either e or f would have to be null... so one may as well have
 separate catches.  If you don't need an instance of the Exception, it
 could work, however D don't support it.

 John

 On Mon, 11 Oct 2004 21:27:27 +0200, Asaf Karagila wrote:

 well, this is some-sort of a request for more examples of using
 try-catch-finally blocks,
 because there is only one simple example on JCC's site and i would like 
 to
 see some more code
 using it, so if possible to have couple more examples about it,
 basicly my problem is that i have a pretty big block to catch all kind of
 errors on, so i would like to know
 how to catch several errors (or just catch them all) in a single/multiple
 catch()..

 Cheers,
 Asaf
well, my code is pretty simple (and probably not too well written either..), the thing is, i came from asm background, so i'm unfimiliar with exceptions, and far from it is the try-catch-finally blocks.. if you would be kind enough to explain further i would be very thankful. Cheers, Asaf
Oct 12 2004
parent Sean Kelly <sean f4.ca> writes:
In article <ckh84k$17v8$1 digitaldaemon.com>, Asaf Karagila says...
well, my code is pretty simple (and probably not too well written either..),
the thing is, i came from asm background, so i'm unfimiliar with exceptions,
and far from it is the try-catch-finally blocks..
if you would be kind enough to explain further i would be very thankful.
An exception is a signal that tells the program to abort whatever it's doing and unroll the stack until it encounters an appropriate exception handler. Here's an example: The above should print: f1 pre exception finally MyException caught, rethrowing exception caught with message: hello world more processing The above code demonstrates a bunch of different things: You can throw anything, but it's most common to throw a class (because they allow a bunch of information to be aggregated). When an exception is thrown, the stack is unwound until an appropriate exception handler is found. An exception will be caught by any handler that expects that specific type or a base of that type (kind of like how you can pass a derived class to a function expecting the base class). Catch statements are evaluated in order, so the main function sees the catch clause for Exception and calls that one, even though there is a more specialized handler further on. Finally blocks are always evaluated whether an exception is thrown or not. If an exception is not caught the program will unroll right through main and exit. This is expected behavior in some cases (assertion failures actually throw an exception of type AssertError). Stack unwinding can be considered to behave the same way as when a function exits normally, so auto classes should have their destructors calls (they currently don't, this is a bug), etc. Exceptions are not resumable. ie. Execution continues at the catch block--it doesn't continue from where the exception was thrown. You can rethrow an exception as per the example in f5. In C++, if two exceptions are thrown simultaneously then the program terminates. I expect this is how D behaves, but some bugs are preventing me from testing it. Here's the canonical example: When "oops!" is thrown, the instance of C is destroyed (because it's auto and has gone out of scope). When this happens C thrown another exception ("die"). Because there's no easy way to figure out how to handle two potentially different errors simultaneously (say the classes are actually different types handled by separate catch blocks), the program terminates. Sean
Oct 12 2004
prev sibling parent "Asaf Karagila" <kas1 netvision.net.il> writes:
is there a way to simply install a global exception handler ?
i think that one global handler would suffice to my problem..

- Asaf 
Oct 15 2004