Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Error Handling Within Classes

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

596 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