Exceptions
As already described in the topic Try - Catch the .NET Framework uses exceptions to notify about an error condition.
Take as sample the String:SubString()
method:
local cString as string local cResult as string cString := "Hello world" cResult := cString:Substring( 5, 30 )
Since the string is less than 35 characters long, this code produces an OutOfRangeException and terminates your program.
So, when using this method on strings, you have a few possibilities:
- use this method only enclosed in a
try-catch
block - check the string length and adjust the parameters accordingly
- or use an own method (for example a static method or an Extension methods of the
System.String
class)
Exception handling is very important in the .NET Framework. In every place where an error can occur, you should provide exception handling. If you don't provide it, at runtime the application will crash without details about the error. There are also a few application wide handlers you can install (and your should use them):
- AppDomain.UnhandledException: handles otherwise unhandled exceptions
- Application.ThreadException: handles exceptions in Windows Forms applications
- Application.DispatcherUnhandledException: handles exceptions in the WPF GUI
You can find a note about these also in the X# forum: Handling exceptions by Paul Piko
There are several publicily available exception handlers like Apache log4net or Exception Handling Application Block in the Enterprise Library.
Nevertheless here is a small collection of simple exception handlers: