Jump to page: 1 2 3
Thread overview
Exception Safe Programming
Feb 24, 2007
Saaa
Feb 24, 2007
Saaa
Feb 25, 2007
Tyler Knott
Feb 25, 2007
Saaa
Feb 25, 2007
renoX
Feb 25, 2007
Saaa
Feb 25, 2007
Frits van Bommel
Feb 25, 2007
Saaa
Feb 25, 2007
Frits van Bommel
Feb 25, 2007
Saaa
Feb 25, 2007
Tyler Knott
Feb 25, 2007
Saaa
Feb 25, 2007
Tyler Knott
Feb 25, 2007
Saaa
Feb 25, 2007
Tyler Knott
Feb 25, 2007
Saaa
[OT] Re: Exception Safe Programming
Feb 25, 2007
Daniel Keep
Feb 26, 2007
Manfred Nowak
Feb 26, 2007
Saaa
Feb 26, 2007
Saaa
Feb 26, 2007
Tyler Knott
Feb 26, 2007
Saaa
February 24, 2007
On the website there is the following example:

Transaction abc()
{
    Foo f;
    Bar b;

    f = dofoo();
    try
    {
    	b = dobar();
    	return Transaction(f, b);
    }
    catch (Object o)
    {
    	dofoo_undo(f);
    	throw o;
    }
}

When f=dofoo() is run and doesn't succeed I suspect that f hasn't changed
and dofoo has thrown an exception.
Because of the exception the try part isn't run, but the catch part is.
Did I understand this correctly?
The explanation of the try statement on the website is not the most
comprehensible one (^.^)



February 24, 2007
"Saaa" <empty@needmail.com> wrote in message news:erqh38$2hen$1@digitalmars.com...
> On the website there is the following example:
>
> Transaction abc()
> {
>    Foo f;
>    Bar b;
>
>    f = dofoo();
>    try
>    {
>    b = dobar();
>    return Transaction(f, b);
>    }
>    catch (Object o)
>    {
>    dofoo_undo(f);
>    throw o;
>    }
> }
>
> When f=dofoo() is run and doesn't succeed I suspect that f hasn't changed
> and dofoo has thrown an exception.
> Because of the exception the try part isn't run, but the catch part is.
> Did I understand this correctly?

Nope.  If "f = dofoo()" fails and an exception is thrown, that catch block is not run, because "f = dofoo()" is outside the try block.  A catch block is only run if an uncaught exception occurs in the try block right before it.  Since "f = dofoo()" is not in a try block, any exception from it will just be thrown out of abc().


February 24, 2007
That is exactly what I originally thought, but why then is that piece of
code a example of how to ...
oh wait... if f=dofoo() fails, f is still unchanged and the correct thing to
do is to exit the function through an exception.
never mind, but thanks :)

>
> Nope.  If "f = dofoo()" fails and an exception is thrown, that catch block is not run, because "f = dofoo()" is outside the try block.  A catch block is only run if an uncaught exception occurs in the try block right before it.  Since "f = dofoo()" is not in a try block, any exception from it will just be thrown out of abc().



February 25, 2007
Saaa wrote:
> That is exactly what I originally thought, but why then is that piece of code a example of how to ...
> oh wait... if f=dofoo() fails, f is still unchanged and the correct thing to do is to exit the function through an exception.
> never mind, but thanks :)

When an exception is thrown and not caught, the compiler unwinds the callstack until it finds a handler.  What this means is that if a function throws an exception then the program will look for the closest call inside a try block with a following catch block that will accept the type of the exception (or a type the exception is implicitly castable to).  So, for example:

import std.stdio;

class MsgClass
{
	this() { writefln("MsgClass constructed."); }
	~this() { writefln("MsgClass destructed."); }
}

class TestException : Exception
{
	this(char[] msg) { super(msg); }
}

void thrower()
{
	writefln("Throwing exception.");
	throw new TestException("This is an exception");
}

int inBetween()
{
	//Destructs after the exception because its scope is destroyed by the exception
	scope MsgClass i = new MsgClass();
	thrower();
	writefln("Returning 5..."); //Never prints
	return 5; //Function never returns
}

void main()
{
	int x;
	try
	{
		x = inBetween(); //x is never assigned
	}
	catch(Exception e)
	{
		writefln("Caught exception: ", e);
	}
	writefln("The value of x is: ", x); //x = 0
}

This code will output the following with thrower() called:

MsgClass constructed.
Throwing exception.
MsgClass destructed.
Caught exception: This is an exception
The value of x is: 0

And this without:

MsgClass constructed.
Returning 5...
MsgClass destructed.
The value of x is: 5
February 25, 2007
Saaa a écrit :
> On the website there is the following example:
> 
> Transaction abc()
> {
>     Foo f;
>     Bar b;
> 
>     f = dofoo();
>     try
>     {
>     	b = dobar();
>     	return Transaction(f, b);
>     }
>     catch (Object o)
>     {
>     	dofoo_undo(f);
>     	throw o;
>     }
> }
> 
> When f=dofoo() is run and doesn't succeed I suspect that f hasn't changed and dofoo has thrown an exception.
> Because of the exception the try part isn't run, but the catch part is.
> Did I understand this correctly?
> The explanation of the try statement on the website is not the most comprehensible one (^.^)

That's probably because this is a counter-example, not an example really: more a (convincing) way to show that scope() is much better than try/catch for exception safe programming..

renoX
February 25, 2007
"renoX" <renosky@free.fr> wrote in message news:errs2s$1pvc$1@digitalmars.com...
> Saaa a écrit :
>> On the website there is the following example:
>>
>> Transaction abc()
>> {
>>     Foo f;
>>     Bar b;
>>
>>     f = dofoo();
>>     try
>>     {
>>     b = dobar();
>>     return Transaction(f, b);
>>     }
>>     catch (Object o)
>>     {
>>     dofoo_undo(f);
>>     throw o;
>>     }
>> }
>>
>> When f=dofoo() is run and doesn't succeed I suspect that f hasn't changed
>> and dofoo has thrown an exception.
>> Because of the exception the try part isn't run, but the catch part is.
>> Did I understand this correctly?
>> The explanation of the try statement on the website is not the most
>> comprehensible one (^.^)
>
> That's probably because this is a counter-example, not an example really: more a (convincing) way to show that scope() is much better than try/catch for exception safe programming..
>
> renoX

I meant the try statement explanation on http://www.digitalmars.com/d/statement.html#TryStatement


February 25, 2007
Saaa wrote:
> "renoX" <renosky@free.fr> wrote in message news:errs2s$1pvc$1@digitalmars.com...
>> Saaa a écrit :
>>> On the website there is the following example:
>>>
>>> Transaction abc()
>>> {
>>>     Foo f;
>>>     Bar b;
>>>
>>>     f = dofoo();
>>>     try
>>>     {
>>>     b = dobar();
>>>     return Transaction(f, b);
>>>     }
>>>     catch (Object o)
>>>     {
>>>     dofoo_undo(f);
>>>     throw o;
>>>     }
>>> }
[snip]
>> That's probably because this is a counter-example, not an example really: more a (convincing) way to show that scope() is much better than try/catch for exception safe programming..
> 
> I meant the try statement explanation on
> http://www.digitalmars.com/d/statement.html#TryStatement 

The only example I see there is:
---
int main()
{
    try
    {
	try
	{
	    throw new Exception("first");
	}
	finally
	{
	    printf("finally\n");
	    throw new Exception("second");
	}
    }
    catch(Exception e)
    {
	printf("catch %.*s\n", e.msg);
    }
    printf("done\n");
    return 0;
}
---
(printf? The horror... :( )
February 25, 2007

>>> That's probably because this is a counter-example, not an example really: more a (convincing) way to show that scope() is much better than try/catch for exception safe programming..
>>
>> I meant the try statement explanation on http://www.digitalmars.com/d/statement.html#TryStatement
>
> The only example I see there is:
> ---

: /
I meant the explanation itself....
My question was originally about an example from the exception safe
programming page.
But I just mentioned that the try statement explanation(on
http://www.digitalmars.com/d/statement.html#TryStatement )
isn't the most comprehensible.(for me that is:)



February 25, 2007
Thanks,
I understand it now: place 'x = inBetween();' before 'try' and the catch
will never run (crash)



>
> When an exception is thrown and not caught, the compiler unwinds the callstack until it finds a handler.  What this means is that if a function throws an exception then the program will look for the closest call inside a try block with a following catch block that will accept the type of the exception (or a type the exception is implicitly castable to). So, for example:
>
> import std.stdio;
>
> class MsgClass
> {
> this() { writefln("MsgClass constructed."); }
> ~this() { writefln("MsgClass destructed."); }
> }
>
> class TestException : Exception
> {
> this(char[] msg) { super(msg); }
> }
>
> void thrower()
> {
> writefln("Throwing exception.");
> throw new TestException("This is an exception");
> }
>
> int inBetween()
> {
> //Destructs after the exception because its scope is destroyed by the
> exception
> scope MsgClass i = new MsgClass();
> thrower();
> writefln("Returning 5..."); //Never prints
> return 5; //Function never returns
> }
>
> void main()
> {
> int x;
> try
> {
> x = inBetween(); //x is never assigned
> }
> catch(Exception e)
> {
> writefln("Caught exception: ", e);
> }
> writefln("The value of x is: ", x); //x = 0
> }
>
> This code will output the following with thrower() called:
>
> MsgClass constructed.
> Throwing exception.
> MsgClass destructed.
> Caught exception: This is an exception
> The value of x is: 0
>
> And this without:
>
> MsgClass constructed.
> Returning 5...
> MsgClass destructed.
> The value of x is: 5


February 25, 2007
Saaa wrote:
>>>> That's probably because this is a counter-example, not an example really: more a (convincing) way to show that scope() is much better than try/catch for exception safe programming..
>>> I meant the try statement explanation on
>>> http://www.digitalmars.com/d/statement.html#TryStatement
>> The only example I see there is:
>> ---
> 
> : /
> I meant the explanation itself....

Ah, I must have read that 'explanation' as 'example'.

> My question was originally about an example from the exception safe programming page.
> But I just mentioned that the try statement explanation(on http://www.digitalmars.com/d/statement.html#TryStatement )
> isn't the most comprehensible.(for me that is:)

Hmm, yes. That section doesn't seem to explain what exceptions *are*, does it? It seems to assume you already know that.
http://www.digitalmars.com/d/errors.html is a bit better, but I'm not sure how clear it is to someone entirely unfamiliar with the concept of exception handling (e.g. someone coming from a purely C background).
« First   ‹ Prev
1 2 3