Rethrow an exception like in C++?

Rob T alanb at ucora.com
Fri Mar 8 12:06:54 PST 2013


On Friday, 8 March 2013 at 19:58:10 UTC, H. S. Teoh wrote:
> On Fri, Mar 08, 2013 at 08:52:21PM +0100, Rob T wrote:
>> On Friday, 8 March 2013 at 06:05:02 UTC, Maxim Fomin wrote:
>> >    catch (Exception e) {
>> >        if (typeid(e) == typeid(myException1))
>> >            throw e; // may be downcasted, if necessary
>> >                     // to work with specific fields
>> >    }
>> 
>> 
>> Isn't it better to check identity in this way?
>> 
>> if (typeid(e) is typeid(myException1))
> [...]
>
> Couldn't you just do this?
>
> 	auto myExc = cast(myException1)e;
> 	if (myExc !is null) {
> 		// do stuff with myExc
> 	}
>
> ?
>
> Or am I missing the point here?
>
>
> T

I didn't know that casting a base class to a derived class that 
the base does not refer to will resolve to a null. Nice to know.

I suppose that's another way to do the same thing which may be 
more convenient  depending the situation.

Thanks for suggesting it.

--rt


More information about the Digitalmars-d-learn mailing list