Exception throwing and guarentees about execution order

D

Dick Brown

Hi

As far as I understand 11.6 (6) of the Ada 95 Reference Manual, Ada
implementations may "lose" information, where exactly an exception has
been raised. I'm not sure whether I understand Ada in that regard, but
that's off-topic anyways (if someone wants to correct me there, I'd be
grateful nonetheless).

Something like:

try
{
int i = 0;
if (...) throw(i);
int i = 1;
if (...) throw(i);
int i = 2;
}
catch (...)
{
// i may be 0 or 1 or 2, no matter which throw(i) has brought us here
}

Now the real question: Does C++ have any similar provisions? I've found
nothing in the standard, am I right there?

Have a nice weekend
Dick
 
I

Ian Collins

Dick said:
Hi

As far as I understand 11.6 (6) of the Ada 95 Reference Manual, Ada
implementations may "lose" information, where exactly an exception has
been raised. I'm not sure whether I understand Ada in that regard, but
that's off-topic anyways (if someone wants to correct me there, I'd be
grateful nonetheless).

Something like:

try
{
int i = 0;
if (...) throw(i);
int i = 1;
if (...) throw(i);
int i = 2;
}
catch (...)
{
// i may be 0 or 1 or 2, no matter which throw(i) has brought us here
}

Now the real question: Does C++ have any similar provisions? I've found
nothing in the standard, am I right there?

I'm not sure what you are asking, catch(...) will catch any exception,
so there isn't any information to "loose".
Have a nice weekend

It's been and gone here!
 
D

Dick Brown

Ian Collins said:
I'm not sure what you are asking, catch(...) will catch any exception,
so there isn't any information to "loose".

Oh, 'catch (...)' was stupid, make that 'catch (int i)'.

Suppose the first throw throws. Is it guaranteed that the caught 'int i'
is zero inside the exception handler?

Dick
 
A

Alf P. Steinbach

* Dick Brown:
Hi

As far as I understand 11.6 (6) of the Ada 95 Reference Manual, Ada
implementations may "lose" information, where exactly an exception has
been raised. I'm not sure whether I understand Ada in that regard, but
that's off-topic anyways (if someone wants to correct me there, I'd be
grateful nonetheless).

Something like:

try
{
int i = 0;
if (...) throw(i);
int i = 1;
if (...) throw(i);
int i = 2;
}
catch (...)
{
// i may be 0 or 1 or 2, no matter which throw(i) has brought us here
}

Now the real question: Does C++ have any similar provisions?

No. C++ exceptions are deterministic and synchronous, except for the possibility
of implementation defined behavior for stack unwinding for an uncaught exception.

I've found
nothing in the standard, am I right there?

No, the standard completely defines the C++ exception handling.


Cheers & hth.,

- Alf
 
R

Rolf Magnus

Dick said:
Oh, 'catch (...)' was stupid, make that 'catch (int i)'.

Suppose the first throw throws. Is it guaranteed that the caught 'int i'
is zero inside the exception handler?

Depends on what you have in your (...) before it. If that changes i, then
no. Otherwise yes. What other value could it have?
 
J

James Kanze

As far as I understand 11.6 (6) of the Ada 95 Reference
Manual, Ada implementations may "lose" information, where
exactly an exception has been raised. I'm not sure whether I
understand Ada in that regard, but that's off-topic anyways
(if someone wants to correct me there, I'd be grateful
nonetheless).
Something like:
try
{
int i = 0;
if (...) throw(i);
int i = 1;
if (...) throw(i);
int i = 2;
}
catch (...)
{
// i may be 0 or 1 or 2, no matter which throw(i) has brought us here
}
Now the real question: Does C++ have any similar provisions?
I've found nothing in the standard, am I right there?

It's hard to say. The above won't compile in C++, so we can't
begin to speak about the behavior it might have. If you change
it to:

int i ;
try {
i = 0 ;
if ( ... ) throw i ;
i = 1 ;
if ( ... ) throw i ;
i = 2 ;
} catch ( ... ) {
}

Then in the catch block, i will have a value of 0 if the first
throw occurred, and a value of 1 if the second occurred. (I'm
not sure about Ada, but I imagine that a lot of languages don't
offer this guarantee---it has a decidedly negative impact on
optimization.)
 
D

DerTopper

Hi

As far as I understand 11.6 (6) of the Ada 95 Reference Manual, Ada
implementations may "lose" information, where exactly an exception has
been raised. I'm not sure whether I understand Ada in that regard, but
that's off-topic anyways (if someone wants to correct me there, I'd be
grateful nonetheless).

Something like:

try
{
  int i = 0;
  if (...) throw(i);
  int i = 1;
  if (...) throw(i);
  int i = 2;}

catch (...)
{
  // i may be 0 or 1 or 2, no matter which throw(i) has brought us here

}

Now the real question: Does C++ have any similar provisions? I've found
nothing in the standard, am I right there?

IMHO, "losing" the information about where exactly an exception has
been raised is what exception handling is all about: If you really
have to know where exactly an error has occurred, you should use error
codes for you functions:
ErrorCodeType DoSomething () throw ();
void SomeFunction ()
{
if (DoSomething () != ERROR_OK)
{
// handling specific to first call.
}
if (DoSomething ()) != ERROR_OK)
{
// handling specific to first call.
}
}

Had you written above code with exceptions, it would look like the
following:
void DoSomething () throw (SomeException);
void SomeFunction ()
{
try
{
DoSomething ();
}
catch (SomeException e)
{
// handling specific to first call.
}
try
{
DoSomething ();
}
catch (SomeException e)
{
// handling specific to second call.
}
}

Note that in this scenario, the code is not very different at all (the
version that works with return types _may_ even be more effective as
no additional exception handling information must be handled at run-
time).

What exceptions are really good for is when you don't really have to
know which particular action of a whole set of actions has failed:
void DoSomething () throw (SomeException);
void DoAnotherThing () throw (SomeException);
void SomeFunction ()
{
try
{
DoSomething ();
DoAnotherThing ();
DoSomething ();
}
catch (SomeException e)
{
// handle any exception.
}
}

AFAIK, exception handling will be same under C++/Java/C#/Ada95. Since
the Ada95 compiler uses C++ code internally, it would be quite
surprising if Ada didn't provide a feature that is present in C++.

Regards,
Stuart
 
J

James Kanze

IMHO, "losing" the information about where exactly an
exception has been raised is what exception handling is all
about:

Yes and no. You generally shouldn't be concerned about the
exact location in itself, but you often have to know how far
you've progressed in order to do a rollback. This is typically
handled by the memento pattern or some variation of transaction
handling, but his code (modified so it makes sense) can be
understood as a simplified stand-in for the memento pattern.

[...]
AFAIK, exception handling will be same under
C++/Java/C#/Ada95. Since the Ada95 compiler uses C++ code
internally, it would be quite surprising if Ada didn't provide
a feature that is present in C++.

There's no fundamental reason to suppose that exception handling
will be the same in different languages (and of course, Ada95
has nothing to do with C++, and there's certainly no requirement
that it use the same compiler). I couldn't find the liberty the
original poster talked about in the Ada standard, but it makes
sense from an optimizer point of view. Given something like:

int i = 0 ;
try {
f1() ;
i = 1 ;
f2() ;
x = f3( ++i ) + f4() ;
} catch ( ... ) {
// ...
}

, on many architectures, it might be advantageous to put i in a
register during certain parts (but not all) of the try block.
If the compiler does this, both C++ and Java require that if an
exception is thrown in f2, i == 1; most of the time, this means
that the compiler will have to write the value back to memory
before any function call (unless that function has an empty
exception specification in C++). Note that Java also guarantees
that if an exception is thrown in f4, i will be equal to 2 in
the catch block; in C++, it's unspecified whether i will be
equal to 1 or to 2. (In other words, exception handling is
different under Java and C++.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top