Topic: catch' clause with multiple arguments proposal
Author: Bart Samwel <bsamwel@WI.leidenuniv.nl>
Date: 1996/09/17 Raw View
Vladimir belkin wrote:
> I think that exception handler wich has 'catch' clause with
> several arguments is a useful construction.
> That exception handler should catch exceptions which are derived from
> ALL its arguments.
How would you determine which of the arguments to the catch clause was
the object actually thrown? And how are you going to fill up the other
arguments: You can't just pass the catch clause empty uninitialized
objects!
Bart
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: belkin@avalon.rosmail.com (Vladimir Belkin)
Date: 1996/09/17 Raw View
Bart Samwel wrote:
>Vladimir belkin wrote:
>
>> I think that exception handler wich has 'catch' clause with
>> several arguments is a useful construction.
>> That exception handler should catch exceptions which are derived from
>> ALL its arguments.
>
>How would you determine which of the arguments to the catch clause was
>the object actually thrown? And how are you going to fill up the other
>arguments: You can't just pass the catch clause empty uninitialized
>objects!
>
> Bart
>
Existing 'catch' clause grammar is:
handler:
catch ( exception declaration ) compound statement
exception declaration:
type specifier seq declarator
type specifier seq abstract declarator
type specifier seq
...
I propose extended 'catch' clause grammar:
handler:
catch ( proposed-exception declaration ) compound statement
proposed-exception declaration:
exception declaration
multiple-exception declaration-list
multiple-exception declaration-list:
multiple-exception declaration
multiple-exception declaration-list , multiple-exception declaration
multiple-exception declaration:
class specifier declarator
class specifier
//////////////
There is no 'other arguments' problem. The handler catchs excepton only if
the class of object actually thrown is derived from _all_ classes
listed in the multiple-exception declaration-list.
Example:
//////////////////////////////////////////////////
struct A { int i; };
struct B { float f; };
struct C { };
struct E { };
struct D:public A,public B,public C {
D() { i=10; f=1.;}
};
void main() {
try {
throw D();
} catch(A a,C c,E) {
// this handler does not catch the exception
// because D is not derived from E.
} catch(A a,B b) {
// exception will be caught here !
cerr<<a.i<<' '<<b.f<<'\n ';
}
}
//////////////////////////////////////////////////
Vladimir
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Vladimir belkin <belkin@avalon.rosmail.com>
Date: 1996/09/07 Raw View
Hello!
It is useful to group exceptions by number of categories
using multiple inheritance. For example, there are two container
classes 'ArrayOf' and 'FileOf' which allow to access data directly.
They both may throw exception derived from 'X_RangeError', that
classes also mark their exceptions as related to their parameter 'T'.
//.....................................................................
struct X_RangeError { int i; };
struct X_IOError { };
template<class T>
struct X_Related {};
template<class T>
class ArrayOf {
public:
struct X:public X_Related<T> {};
struct X_RangeError:public X,public ::X_RangeError {};
T &operator [] (int i) throw(X_RangeError);
};
template<class T>
class FileOf {
public:
struct X:public X_Related<T> {};
struct X_Seek:public X,public X_RangeError,X_IOError {};
void seek(int offset) throw(X_Seek);
};
//.....................................................................
Then I want to catch all exceptions which are derived from both
'X_Related<int>'
and 'X_RangeError'.
I don't want to have special classes like 'X_RangeErrorRalated<X>',
because there may be very large number of combinations like this.
Now I write something like:
// .............................................
try {
// ... do something
} catch (X_Related<int>) {
try { throw; }
catch (X_RangeError x) {
// do something
}
}
// .............................................
It may be better to write code like this as:
try {
// ... do something
} catch(X_Related<int>,X_RangeError x)
// Existing C++ does not allows this construction
{
// ... do something
}
But existing syntax of 'catch' clause does not allow to
specify more than one argument.
I think that exception handler wich has 'catch' clause with
several arguments is a useful construction.
That exception handler should catch exceptions which are derived from
ALL its arguments.
What do you think of this problem?
Regards,
Vladimir (belkin@avalon.rosmail.com)
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]