Beginners guide to .NET Error Handling
Error Handling For .Net is done using try..throw..catch..finally commands for both C# and VB.NET.
public void SomeFunction()
{
try
{
throw new ArgumentException("Error Message");
throw new ArgumentException("Error Message", new FormatException("Inner Exception"));
}
catch (ArgumentException ex)
{
// This block will catch all ArgumentExceptions thrown by the code.
}
catch (Exception Ex)
{
// This block will catch all non-ArgumentException Exceptions.
}
finally
{
}
}
An exception can have different parameters when throwing an exception. For example, ArgumentException has five overloaded parameter lists, whereas the FormatException has only three. When throwing an exception, you are, in most cases, able to pass a message to the exception and also an Inner Exception so to produce more information. In the above example, it should be noted that only one exception will be raised as the second throw statement will not be executed because the code will have gone to the correct exception block.
Once the exception handling code has been executed, the code in the finally block will be executed. The finally code will have to be called whether there have been errors or not. It is the place to put all your tidying-up code.
If an error occurs in a function with no error handling, the error will be passed to the calling function. It will continue to exit functions until either the error is handled in an error handler or the program crashes.
One of the neat things about .NET is the ability to break when a particular error occurs. In VB6, one of the irritating things about the language was that when you wanted to see if a collection contained an item, you would have to wrap the checking of existence in an On ERROR RESUME NEXT. If the error was 5 then ignore. It worked fine. Still, if you were trying to track down a different error in a complex function that repeatedly checked for the existence of an item in a collection, would be very time-consuming. .NET allows you to turn on errors so that when they occur, the code breaks. So if we turned on ArgumentException and ran the above code, the code would break on the first throw statement. If we turned off ArgumentException, turned on FormatException, commented out the first throw, and ran the code, the code would break on the throw statement. If we turned off both exceptions, the code would execute normally. To turn on and off errors, uses Ctrl+Alt+E.
It is possible to create your specialised error objects. Below is an example of developer-created error class as opposed to one created by Microsoft.
public class FileNotFound : ApplicationException
{
private string mlngFileName;
public FileNotFound(string Message) : base(Message)
{ }
public FileNotFound(string Message, Exception Inner) : base(Message, Inner)
{ }
public FileNotFound(string Message, Exception Inner, string FileName) : base(Message, Inner)
{
mlngFileName = FileName;
}
public string FileName
{
get { return mlngFileName; }
}
}
If you want a VB.NET example, have a look at Microsoft Library where you might notice the code is similar. You'll be right. I converted and tweaked it a little. Then to use the code, all you would do is throw it as in:-
throw new FileNotFound("File not found", null, "C:\temp\File.txt");
catch(FileNotFound ex)
{
console.writeline("Could not find " + ex.FileName");
}
Tags - .NET
Last Modified : June 2023