?
Solved

Error Handling Within Classes

Posted on 2006-07-10
4
Medium Priority
?
186 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 1000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month9 days, 22 hours left to enroll

762 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