Welcome to Manicprogrammer Sign in | Join | Help

Writing better exception Strikes Back!

Hmmm, as pinpointed by my good friend Claudio Figueiredo, the compiler isn't very thrilled with my last syntax:

   1:  public string MyCode(){
   2:    try{
   3:      return "some value";
   4:    }
   5:    catch(System.Exception ex){
   6:      Throw
   7:           .WithMessage("something")
   8:           .WithInnerException(ex)
   9:           .Exception<InvalidOperationException>();
  10:    }
  11:  }

You actually get a compiler error saying you need to return something, or something like that. That's because there's no way of telling dumb ol' compiler that the Throw.Exception<ExceptionType> method is actually a replacement for throw.

Ok, after I got over being angry at the compiler, I decided on having a different syntax (not as neat as the first one, but accomplishes the same):

   1:  throw NewException.WithMessage("Something happened")
   2:                    .Because("Why did this happen")
   3:                    .WithWorkaround("What can I do about it")
   4:                    .Of<InvalidOperationException>();

Hope it helps!

#142

Published Tuesday, May 06, 2008 9:09 PM by heynemann

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Writing better exception Strikes Back!

Wednesday, May 07, 2008 5:17 AM by Jaco

I would just like to point out that you are not returning the original exception as an inner exception - best practice dictates that you should NEVER destroy the original exception - ALWAYS include it as an inner exception.  It seems that your model can do it, your example simply doesn't.

# re: Writing better exception Strikes Back!

Wednesday, May 07, 2008 11:01 AM by heynemann

That's because in my examples, I'm throwing exceptions out of the blue. I'm not just catching and rethrowing.

I'll show in an example:

public void DoSomethingWith(string myArgument){

 if (string.IsNullOrEmpty(myArgument)){

   throw NewException.WithMessage("My Argument must be filled!".Because("My method needs to do something with it!").WithWorkaround("pass in a valid non-empty string").Of<InvalidOperationException>();

 }

 //Do something

}

You can see that in this scenario I don't mean to actually "repackage" an exception. That's why you're not FORCED to include an inner exception (and that doesn't happen with regular throws as well).

Thank you very much for your comment, though!

Happy Coding!

Bernardo Heynemann

# Amazing Results

Wednesday, May 07, 2008 4:41 PM by while(availableTime>0) {

It's been only 2 days after I first implemented my new exception throwing mechanism , and I already feel

# re: Writing better exception Strikes Back!

Thursday, May 08, 2008 11:15 AM by Ben

Hi,

How'd you get around the implicit conversion problem?

From memory that failed for me, i.e., attempting to throw the ExceptionBuilder failed, even though I implemented conversion operator.

Also, I'm using Activator to instantiate the exceptions, I guess the performance penalty is minor given that we're already in the context of an exception...(?)

# re: Writing better exception Strikes Back!

Thursday, May 08, 2008 6:08 PM by heynemann

I'm sorry Ben, I'm not sure about what Implicit conversion problem you mean.

I just return a new exception with whatever type you specified in the Of<ExceptionType> clause.

I'm also using Activator, and I am PRETTY sure it doesn't matter. I read last month a comparison of creating like 1 million instances with Activator and it takes like 3 seconds to create a million (or something like that).

Cheers,

# re: Writing better exception Strikes Back!

Friday, May 09, 2008 4:07 AM by Ben

Hi again,

So does Of explicitly return an Exception?

I tried doing it by having my ExceptionBuilder <a href="http://www.google.co.uk/url?sa=t&ct=res&cd=1&url=http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fz5z9kes2(VS.71).aspx&ei=6QUkSLegN6aM0QTQt52qCw&usg=AFQjCNElfsE61TNqK3g46vLPXdFkMmRdhw&sig2=jQ9tXpSFC3n3IzRQlCg2Rw">implicitly</a> convertible to Exception, but throwing it didn't compile.

<bb />

# re: Writing better exception Strikes Back!

Friday, May 09, 2008 7:17 AM by Bernardo Heynemann

Enter the text you see in the image:

Leave a Comment

(required) 
required 
(required)