Catching exception types given as type parameters in C# 2.0?

See this thread for more info: stackoverflow.com/questions/1577760/why-... Seems to be a bug: https://connect.microsoft. Com/VisualStudio/feedback/ViewFeedback. Aspx?

FeedbackID=362422&wa=wsignin1.0.

Works fine for me in this test: using System; using System. IO; class Test { static void Main() { Throws(() => File. OpenText(null)); } public static void Throws(Action cmd) where ExceptionType : Exception { try { try { cmd(); } catch (ExceptionType) { Console.

WriteLine("Caught! "); return; } } catch (Exception f) { Console. WriteLine("Threw an exception of type " + f.GetType() + ".

Expected type was " + typeof(ExceptionType) + ". "); } Console. WriteLine("No exception thrown"); } } How certain are you that the two AssertionException exceptions are identical?

Are they definitely in the same namespace (print typeof(ExceptionType) rather than just the Name property)? Are they from the same assembly? I wouldn't be surprised to find some oddities due to multiple test framework versions coexisting... Try it with an exception other than AssertionException to make life simpler.

I haven't debugged this code or looked at it too long but could it be that you first "return" statement is causing the code to HALT and leave the method. Therefore, the exception is "caught" or "handled" (however you look at it) and the outer try/catch block is never executed. Are you trying to do this?

Try { try { cmd.Execute(); } catch (ExceptionType) { throw; } } catch (Exception f) { throw new AssertionException(cmd.ToString() + " threw an exception of type " + f.GetType() + ". Expected type was " + typeof(ExceptionType). Name + "."); }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions