C# Exception handling 101
-
Here's a 'nice' pattern I saw in some code I've inherited...
catch (Exception ex)
{
if (ex is ExceptionSpecialType)
throw new SomeException()
{
CustomData = (ex as ExceptionSpecialType).SomeThing.ToXmlString()
};
else
{
// <-- Some specific handling removed here -->
throw new SomeException()
{
CustomData = new CustomError(ex).ToXmlString()
};
}
}Oh, and yes, my predecessor loved using 'if (X is T) (X as T).M();'.
'I'm French! Why do you think I've got this outrrrrageous accent?' Monty Python and the Holy Grail
-
Here's a 'nice' pattern I saw in some code I've inherited...
catch (Exception ex)
{
if (ex is ExceptionSpecialType)
throw new SomeException()
{
CustomData = (ex as ExceptionSpecialType).SomeThing.ToXmlString()
};
else
{
// <-- Some specific handling removed here -->
throw new SomeException()
{
CustomData = new CustomError(ex).ToXmlString()
};
}
}Oh, and yes, my predecessor loved using 'if (X is T) (X as T).M();'.
'I'm French! Why do you think I've got this outrrrrageous accent?' Monty Python and the Holy Grail
And I thought my custom unrecoverable Exception handling wrapper sucked:
Helper.logUnrecoverable(this.GetType().ToString(), "doSync - createHeader or createLine fail. Action rollbacked", e.Message, true);
Where
true
is whether it should log the StackTrace or not. But this is a whole different :wtf:-ness levelFull-fledged Java/.NET lover, full-fledged PHP hater. Full-fledged Google/Microsoft lover, full-fledged Apple hater. Full-fledged Skype lover, full-fledged YM hater.
-
Here's a 'nice' pattern I saw in some code I've inherited...
catch (Exception ex)
{
if (ex is ExceptionSpecialType)
throw new SomeException()
{
CustomData = (ex as ExceptionSpecialType).SomeThing.ToXmlString()
};
else
{
// <-- Some specific handling removed here -->
throw new SomeException()
{
CustomData = new CustomError(ex).ToXmlString()
};
}
}Oh, and yes, my predecessor loved using 'if (X is T) (X as T).M();'.
'I'm French! Why do you think I've got this outrrrrageous accent?' Monty Python and the Holy Grail
Obviously, your predecessor did not have the slightest understanding of proper exception handling. Even reading one article on Code Project about exception would have helped him greatly :-D
Just because the code works, it doesn't mean that it is good code.
-
Here's a 'nice' pattern I saw in some code I've inherited...
catch (Exception ex)
{
if (ex is ExceptionSpecialType)
throw new SomeException()
{
CustomData = (ex as ExceptionSpecialType).SomeThing.ToXmlString()
};
else
{
// <-- Some specific handling removed here -->
throw new SomeException()
{
CustomData = new CustomError(ex).ToXmlString()
};
}
}Oh, and yes, my predecessor loved using 'if (X is T) (X as T).M();'.
'I'm French! Why do you think I've got this outrrrrageous accent?' Monty Python and the Holy Grail
I've worked with a codebase where each and every method has a try catch block. The catch logged the error and that's that. As a result, every method you called would return normally like everything went fine. Something like an Exception or unexpected results when updating a database record could very well be the result of a ReferenceNull- or InvalidCastException somewhere in some method. Good luck trying to find it :)
It's an OO world.
public class Naerling : Lazy<Person>{
public void DoWork(){ throw new NotImplementedException(); }
} -
I've worked with a codebase where each and every method has a try catch block. The catch logged the error and that's that. As a result, every method you called would return normally like everything went fine. Something like an Exception or unexpected results when updating a database record could very well be the result of a ReferenceNull- or InvalidCastException somewhere in some method. Good luck trying to find it :)
It's an OO world.
public class Naerling : Lazy<Person>{
public void DoWork(){ throw new NotImplementedException(); }
}"Look boss, never any error!" In Visual Studio, you can set a kind of breakpoints on exceptions just when they're thrown, I guess it could help with that kind of monstrous codebase.
'I'm French! Why do you think I've got this outrrrrageous accent?' Monty Python and the Holy Grail
-
"Look boss, never any error!" In Visual Studio, you can set a kind of breakpoints on exceptions just when they're thrown, I guess it could help with that kind of monstrous codebase.
'I'm French! Why do you think I've got this outrrrrageous accent?' Monty Python and the Holy Grail
Julien Villers wrote:
it could help with that kind of monstrous codebase
We would sit and cry in the corner waiting for help... Help never came ;p
It's an OO world.
public class Naerling : Lazy<Person>{
public void DoWork(){ throw new NotImplementedException(); }
} -
Here's a 'nice' pattern I saw in some code I've inherited...
catch (Exception ex)
{
if (ex is ExceptionSpecialType)
throw new SomeException()
{
CustomData = (ex as ExceptionSpecialType).SomeThing.ToXmlString()
};
else
{
// <-- Some specific handling removed here -->
throw new SomeException()
{
CustomData = new CustomError(ex).ToXmlString()
};
}
}Oh, and yes, my predecessor loved using 'if (X is T) (X as T).M();'.
'I'm French! Why do you think I've got this outrrrrageous accent?' Monty Python and the Holy Grail
WPF by any chance? I was forced to write code like that for it.
He who asks a question is a fool for five minutes. He who does not ask a question remains a fool forever. [Chineese Proverb] Jonathan C Dickinson (C# Software Engineer)
-
WPF by any chance? I was forced to write code like that for it.
He who asks a question is a fool for five minutes. He who does not ask a question remains a fool forever. [Chineese Proverb] Jonathan C Dickinson (C# Software Engineer)
Nope. I'm mostly working in Silverlight, but this is backend code for a webservice, so ASP.NET. I don't see any reason to 'force' you to write bad exception handling code, ie catching Exception, then testing type.
'I'm French! Why do you think I've got this outrrrrageous accent?' Monty Python and the Holy Grail