Universe Guide

Home / Facts

Error Handling

Error handling for .NET is done using try..throw..catch..finally commands for both C# and VB.NET.
 public void SomeFuction() { 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 { } }
When throwing an exception, an exception can have different parameters, for example ArgumentException has five overloaded parameter list 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 excuted 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 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 of the language was that when you wanted to see if a collection contained an item, you would have to wrap the checking of existance in an On ERROR RESUME NEXT and if the error was 5 then ignore. It worked fine but if was you trying to track down a different error in a complex function that repeatedly checked for the existance of an item in a collection, it 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 and turned on FormatException and commented out the first throw then ran the code, the code would break on the throw statement. If we turned off both exception, the code would excute normally. To turn on and off errors, use Ctrl+Alt+E.

It is possible to create your own 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"); } 

Last Updated :

Related Pages of Interest :-

Add a Comment

Email: (Optional)