Want to follow this site? Here's the RSS feed.
October 2005 Table of Contents
.NET 2.0 try{}catch{} and try{}catch(Exception ex){}
Sunday, October 23, 2005
One thing that I found rather interesting about .NET 2 that's different from .NET v1 is how v2 handles non-exception throwables differently.
First, a little background...
In case you don't know, the following two pieces of code are completely different.
try { Thrower.Start( ); } catch (System.Exception ex) { System.Console.Write("Error!"); }
try { Thrower.Start( ); } catch { System.Console.Write("Error!"); }
The difference is not that the first catches exceptions from unmanaged code and the second second doesn't, the first actually does catch those exceptions. The difference is also a bit more important than the mere fact that in the first one case we capturing the exception data in an exception type and in the other we aren't. The real difference stems from the fact that that in .NET you can throw things other than exceptions and in v1 you used the latter code to catch those non-exception throwables.
Here's an example of how to throw a non-exception. Yeah, you basically have to use the IL, or a compiler that doesn't check for this stuff.
// ThrowerLib.il .assembly ThrowerLib { } .class public Thrower { .method static public void Start( ) { ldstr "Oops" throw ret } }
Here we are throwing "Oops". That's obviously a string, not something derived from System.Exception. In v1 you could use try{}catch(Exception ex) {} as a catch all for all exceptions, but that would not catch this error as it's not an exception. To catch this, you would also have a catch{} at the end.
OK, so how's v2 different? In this world, the non-exception item thrown is wrapped up in a real exception. So, in .NET v2 you can actually use your normal try {} catch(Exception ex){ } to also catch these non-exception throwables as well.
They are actually thrown as a RuntimeWrappedException. So the following app compiled an run in .NET v1 would explode into pieces, but would would gracefully end in .NET v2.
// ThrowerLib.il .assembly ThrowerLib { } .class public Thrower { .method static public void ThrowException( ) { ldstr "ThrowException exception from the IL world!" newobj instance void [mscorlib]System.Exception::.ctor(string) throw ret } .method static public void ThrowString( ) { ldstr "Weird exception!" throw ret } }
// ThrowerHarness.cs namespace ThrowerExample { class ThrowerHarness { static void Main(string[] args) { try { Thrower.ThrowException( ); } catch (System.Exception ex) { System.Console.WriteLine("System.Exception error: " + ex.Message); } catch { System.Console.WriteLine("Non System.Exception based error."); } try { Thrower.ThrowString( ); } catch (System.Exception ex) { System.Console.WriteLine("System.Exception error: " + ex.Message); } catch { System.Console.WriteLine("Non System.Exception based error."); } } } }
"Common Language Runtime detected an invalid program"
Sunday, October 23, 2005
If you've ever gotten this error you've probably been one of the most confused people in the world. In your debugging you may have seen that this message came from an InvalidProgramException.
Well, while this problem should be rare, here's an example harness of where you may see it.
// ThrowerHarness.cs namespace ThrowerExample { class ThrowerHarness { static void Main(string[] args) { try { Thrower.Start( ); } catch (System.Exception ex) { System.Console.Write("Error: " + ex.Message); } } } }
Alright, so where's it coming from?
In this case, it's actually coming from the IL.
// Thrower.il .assembly ThrowerLib { } .class public Thrower { .method static public void Start( ) { stloc.0 throw ret } }
This actually comes from invalid IL. In this case I'm putting 0 on the stack and then throwing...well, nothing really. This is not something a good compiler would create. If you see this it's probably a bug in the compiler or manually written IL.
Anyhow, to test the above do this...
ilasm /dll ThrowerLib.il csc /r:ThrowerLib ThrowerHarness.cs
.NET Course I'm teaching
Sunday, October 16, 2005
So, for a while now I've been teaching a .NET 2.0/C# 2.0/Object-oriented desing class and decided to come up with a simple class website.
I'm covering not only the basics, but also many advanced topics that you would only find in footnotes. Mainly I'm going to be covering stuff that I wish someone would explain to me in plain english!! So, I'm going to be spending time explaining the differences between ref and out, const and readonly, and try{}catch{} and try{}catch(Exception ex){} among other topics. You can bet that I'll be spending A LOT of time just on delegation!!
There are actually a few series going on at once: C# Language (Intro, Intermediate, Advanced), .NET Framework and Design, Object Orientation and Design Patterns, SOA and Enterprise Development, ASP.NET 2.0 (web-standards "tableless"/CSS!!!), and WinFX concepts are the series all going on in parallel.
As far as the intro ASP.NET stuff is concerned, there are great ASP.NET books out for that kind of stuff. I'm mainly focusing on the middle layer and more advanced concepts. I'm actually planning a lecture for Microsoft Atlas. Given my love for Firefox, .NET, and remote scripting (err, Ajax), that should be fun! Also in the course of things I plan on having a lecture to discuss .NET design guidelines and another lecture on the architecture and mechanics of the CLR! .NET assembly language (yeah, yeah, the IL) rules!
Now what is my supplemental book recommendation? Basically anything that Troelsen has written. His latest book "Pro C# 2005 and the .NET 2.0 Platform" is written incredibly eloquently.
I absolutely love .NET... and this should show! maybe I can get an MVP out of this :D hint hint!!
Anyhow, here's the page I threw togther for it. I'll be posting more information, links, and samples as time goes along.http://www.davidbetz.net/dotnetcourse/




