473,408 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Inconsistence! .NET 2.0 Generics and Exceptions

Hello!

Exception handling in generics seems to be a bit inconsistent to me.

Imagine, I have "MyOwnException" class derived from "ApplicationException".
I also have two classes "ThrowInConstructor" and "ThrowInFoo". First one
throws "MyOwnException" in constructor, second one in "Foo()" method. There
is a "GenericCatch" generics class able to accept "ThrowInConstructor" and
"ThrowInFoo" as type parameter "<T>". There are two methods in
"GenericCatch": "CatchInFoo()" and "CatchInConstructor()". Each method has a
"try{}catch(MyOwnException)" block. Object of type "T" created inside try
block and "Foo()" method is called for it.

----------
The INCONSISTENCE is:

Exception "MyOwnException" thrown from constructor of "ThrowInConstructor",
somehow TURNS INTO "TargetInvocationException" and is NOT caught by
"catch(MyOwnException)" block!!!
----------

There are four possible choices:

1. "catch" block is placed somewhere in class without generics.

1.1 Exception "MyOwnException" thrown in ordinary "Foo()" method falls into
"catch(MyOwnException)" block.
1.2 Exception "MyOwnException" thrown in constructor falls into
"catch(MyOwnException)" block.

2. "catch" block is placed somewhere in class WITH generics.

2.1 Exception "MyOwnException" thrown in ordinary "Foo()" method falls into
"catch(MyOwnException)" block.
2.2 Exception "MyOwnException" thrown in constructor turn into
"TargetInvocationException" (with "InnerException" property set to
"MyOwnException") and DOES NOT fall into "catch(MyOwnException)" block.

Any ideas WHY ??? May be I miss something ???

P.S.
You can find an example (simple C# console application) that illustrates
this issue below.

Thanks,
Vladimir

//
// .NET Framework 2.0
// Microsoft Visual Studio 2005
// C# console application
//

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace generic.exception
{
interface IFoo
{
void Foo();
}
class MyOwnException : ApplicationException
{
public MyOwnException( string message ) : base( message ) {}
}
class GenericCatch< T > where T : class, IFoo, new()
{
public static void CatchInFoo()
{
try
{
T t = new T();
t.Foo();
}
catch ( MyOwnException e )
{
Console.WriteLine( "GenericCatch.CatchInFoo catch (
MyOwnException )" );
Console.WriteLine( "e.Message: " + e.Message );
}
}
public static T CatchInConstructor()
{
try
{
T t = new T();
t.Foo();
}
catch ( MyOwnException e )
{
Console.WriteLine( "GenericCatch.CatchInConstructor catch (
MyOwnException )" );
Console.WriteLine( "e.Message: " + e.Message );
}
catch ( ApplicationException e )
{
Console.WriteLine( "GenericCatch.CatchInConstructor catch (
ApplicationException )" );
Console.WriteLine( "Exception of type: " + e.GetType().ToString() );
Console.WriteLine( "e.Message: " + e.Message );
}
return null;
}
}
class ThrowInConstructor : IFoo
{
public ThrowInConstructor()
{
Console.WriteLine( "ThrowInConstructor.Constructor() throw new
MyOwnException()" );
throw new MyOwnException( "MyOwnException in Constructor()" );
}
public void Foo()
{
}
}
class ThrowInFoo : IFoo
{
public ThrowInFoo()
{
}
public void Foo()
{
Console.WriteLine( "ThrowInFoo.Foo() throw new MyOwnException()" );
throw new MyOwnException( "MyOwnException in Foo()" );
}
}
class Program
{
static void Main( string[] args )
{
try
{
Console.WriteLine( "Call ThrowInConstructor outside of generics" );
ThrowInConstructor t = new ThrowInConstructor();
}
catch( MyOwnException e )
{
Console.WriteLine( "outside of generics catch ( MyOwnException )" );
Console.WriteLine( "e.Message: " + e.Message );
}
Console.WriteLine();
GenericCatch<ThrowInFoo>.CatchInFoo();
Console.WriteLine();
GenericCatch<ThrowInConstructor>.CatchInConstructo r();
}
}
}

/*
Console application output:
----------

Call ThrowInConstructor outside of generics
ThrowInConstructor.Constructor() throw new MyOwnException()
outside of generics catch ( MyOwnException )
e.Message: MyOwnException in Constructor()

ThrowInFoo.Foo() throw new MyOwnException()
GenericCatch.CatchInFoo catch ( MyOwnException )
e.Message: MyOwnException in Foo()

ThrowInConstructor.Constructor() throw new MyOwnException()
GenericCatch.CatchInConstructor catch ( ApplicationException )
Exception of type: System.Reflection.TargetInvocationException
e.Message: Exception has been thrown by the target of an invocation.
*/
May 29 '06 #1
1 2411
Vladimir,
----------
The INCONSISTENCE is:

Exception "MyOwnException" thrown from constructor of "ThrowInConstructor",
somehow TURNS INTO "TargetInvocationException" and is NOT caught by
"catch(MyOwnException)" block!!!
----------


That's because

new T()

compiles to

Activator.CreateInstance<T>()

and that method is documented as throwing a TargetInvocationException
if the constructor throws.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
May 29 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ken | last post by:
Which is the correct means of transferring session_id() attached to the URL address? or do all work? I am getting inconsistence results. $sessionid = session_id(); <a href="<?PHP echo...
1
by: Gabriele Zannoni | last post by:
Hi to all, I have an aesthetic problem to solve. I'm working on the managing part of a site. Some pages can be called with a query string like this: http://mysite.com/manage.aspx?code=300 The...
14
by: Robert Brinson | last post by:
The company I am working for is trying to work with the Application Data Blocks that Microsoft released to provide transparent database connectivity for our .NET applications. I have been tasked...
4
by: bughunter | last post by:
I'm sorry but previously command ALTER TABLE tbl ACTIVATE NOT LOGGED INITIALLY on table created without NOT LOGGED INITIALLY option was impossible. Or not? IMHO, more better give error or...
5
by: Doug Gunnoe | last post by:
I'm considering resizing a div onload to better match the screen width of the user. Easy enough, however it seems that I have read in this group that there are potential problems with this,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.