Tip: Error Handling in .NET

In .NET development the common approach to exception handling is to make use of Try...Catch...Finally.

try {
  // some code
}
catch (NullReferenceException ex)
{
   // handle the exception
}

Sometimes you don't need to handle the exception and you just want to throw it to the calling application

try {
  // some code
}
catch (NullReferenceException ex)
{  
   throw ex;
}

But, if you do that you will get useless messages while debugging your application.

The better approach is to just call throw without "ex"...

try {
  // some code
}
catch (NullReferenceException)
{  
   throw;
}

This now provides the following error message which is much more useful.

Tags:

 

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading