Solved

Error Handling Within Classes

Posted on 2006-07-10
4
179 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now