Solved

Error Handling Within Classes

Posted on 2006-07-10
4
183 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

688 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