|Structured Exception Handling in .NET (ApplicationException)
|by Arnaldo Sandoval |
I began my run in the IT field back in 1975 while at the University getting my Industrial Engineer Bachelor Degree; using Wang Basic language and Burroughs 6700's FORTRAN and COBOL, working as a Teacher’s assistance for a fist full of dollars, since then I had worked in four different countries: Venezuela, USA, Mexico and Australia, the Business Basic Language the main skill expected from me; learning Unix, C, PASCAL, Uniplex, WordPerfect, 20/20 in the lates 80s, Sybase in the early 90s, Basis BBx in the 90s, Microsoft’s VBA in the mid 90s, Visual Basic version 3.0 around 1996; moved to Australia in the lates 90s, here I kept learning, Transoft’s Universal Business Language, Oracle, Microsoft SQL Server, Visual Basic .Net; working for a major Australian company in the building material market. I had worked for Computers Manufacturing companies (MAI Basic Four after reading the Wikepedia definition, it feels good being an active part of the industry), and high rollers companies (DOLE, Pepsico when they operated Pizza Hut, KFC, Taco Bell and PFS, Boral Limited), roaming the world while doing so, exposed to cutting edge technologies of their time, creating it when the opportunity required so. I currently look after an Oracle data warehouse, sourcing its data from four or five legacy applications, servicing Crystal Reports and Cognos Cubes, developed VB and Net solutions; I could claim the phrase “I had been there, done that” suits me like a globe, always addressing any challenge with an engineer mind, which is different to an IT mindset.
View all articles by Arnaldo Sandoval...
Using the ErrorHandlingException Class (ii)
This time the SystemException Catch block is throwing an Application Exception using our class and passing the System Exception message se.Message and the exception itself as inner exception se to the ErrorHandlingException class By doing this, we can trap the application exception using the Catch ehe As SEHComponent.ErrorHandlingException code.
The method ShownError() is part of the demo project. You can check it out by downloading the attached project to this article. It basically display a pop-up window describing the exception.
The code snippets shown so far feature reference to the ErrorHandlingException class by fully qualifying its namespace, but you can avoid too much typing by importing its name space with this line of code at the top of the project using it:
Once this is done, the throw and catch statements referencing it become:
|Throw New ErrorHandlingException("Division", _ |
"Trying to divide by zero")
and / or
Catch ehe As ErrorHandlingException
ENQUIRIES TO THE EXECEPTION LOG
The ErrorHandlingException class as explained on the first and second pages of this article expose a GetExceptions() method returning a DataTable object. You can use this method to provide a quick way to view all the exceptions your application is throwing by adding a data grid to a Windows form or Web Page. As the class does not have a presentation layer, you can use it on either Windows or Web applications.
The image below illustrates the enquiry we are talking about.
The image identifies the application as Test_ErrorHandling but because posting that project into this article was not possible we renamed the application to TestEHE.
The behind the View Exceptions button shown in the picture follows:
|Dim SEH As New ErrorHandlingException |
Dim dt As DataTable
dt = SEH.GetExceptions("")
If Not dt Is Nothing Then
dgExceptions.DataSource = dt
dgExceptions.ReadOnly = True
The GetExceptions method accepts a single parameter, the connection string for the database we are using to Save our exceptions errors. As we are passing an empty string, the class will use the default connection string key, and retrieve the connection string defined at the Application Config file as documented above.
The project used to test the ErrorHandlingException class is attached to this post. You can download it to get a better understanding on how to use the class and get familiar with its behavior.
Its Plain Untrapped Exception and Plain Untrapped Exception (Using Imports) buttons do not display any pop-up message, they just save the exceptions to the Access database.
You should copy the Access database TargetErrorLog.mdb located at its Data folder to the C:\Temp folder once you unzip the project, or fix the Application Config file to point to its location on your PC.
The project has a reference to the ErrorHandlingException.dll binary. This reference might be broken once you unzip the projects posted on this thread and you should fix the reference before trying to use it.
- You can extend the ApplicationException class provided by the .Net Framework, implementing additional functionalities that certainly will benefit your application.
- You can use any .Net development language to implement your ApplicationException class, either C# or VB.Net without major concerns, as you can use your customized exception handling object in any project regardless of the language of preference at your organization.
- You may extend the System Exception class, but it is Microsoft remendation that any extention to Exception classes, be done off the Application Exception class.