Solved

Error Handling Within Classes

Posted on 2006-07-10
4
180 Views
Last Modified: 2010-04-23
I have written a class that contains some methods.  I would like to add error handling for some of the methods and have the error 'bubble-up' to the program which called the method within the class.

What is the best way to accomplish this?

Please provide an example.
0
Comment
Question by:Ray Turner
  • 2
4 Comments
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 17078487
you can use the Try...Catch..Finally construct in your class, and in the Catch block, you can Raise your own error, which will then be 'caught' one level up in the call stack.

The Raise method will generate a Run-time error, where you can supply the error number to be raise, and the Description (this will create a new instance of the Exception class, which you can control in your code.

AW

0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 17078502
correction, that should be Throw your own exception, as shown here:

   Public Shared Sub Main()
   Dim fs As New FileStream("data.txt", FileMode.Open)
      Try
         'Opens a text file.
         Dim sr As New StreamReader(fs)
         Dim line As String
         'A value is read from the file and output to the console.
         line = sr.ReadLine()
         Console.WriteLine(line)
      Catch e As FileNotFoundException
         Console.WriteLine("[Data File Missing] {0}", e)
         Throw New FileNotFoundException("[data.txt not in c:\dev directory]", e)
      Finally
         fs.Close()
      End Try
   End Sub 'Main

AW
0
 
LVL 14

Accepted Solution

by:
ptakja earned 250 total points
ID: 17078648
Or you can throw the exception that was caught instead of creating another exception object which takes a lot of time and is unnecessary.

AW's post would then look like this:

  Public Shared Sub Main()
   Dim fs As New FileStream("data.txt", FileMode.Open)
      Try
         'Opens a text file.
         Dim sr As New StreamReader(fs)
         Dim line As String
         'A value is read from the file and output to the console.
         line = sr.ReadLine()
         Console.WriteLine(line)
      Catch e As FileNotFoundException
         Console.WriteLine("[Data File Missing] {0}", e)
         Throw e
      Finally
         fs.Close()
      End Try
   End Sub 'Main

Or...even simpler:

  Public Shared Sub Main()
   Dim fs As New FileStream("data.txt", FileMode.Open)
      Try
         'Opens a text file.
         Dim sr As New StreamReader(fs)
         Dim line As String
         'A value is read from the file and output to the console.
         line = sr.ReadLine()
         Console.WriteLine(line)
      Catch
         Throw
      Finally
         fs.Close()
      End Try
   End Sub 'Main
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17078905
Good call, ptakja. It's always better to throw the original exception again.

A note of clarification... this is EXCEPTION handling, not error handling. Errors (i.e. predictable issues, like a file not existing) should be handled using mechanisms other than exceptions. Any time you can avoid the overhead of having the exception handling in place (i.e. the try/catch block) you're well-advised to do so.
0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question