Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Can anybody point to some resources about building my own error handling routines?

What I have is a long process that involves calling several methods. During each method something could go wrong, in which case I want to avoid doing the remaining methods.

What I would like would be a centralized error handler that could flip a boolean to let me know if something went wrong. i would then have to test this boolean before each method.

Another reason I want to have this is because I see that I am writing the same 5 lines of code in each of my error handlers and anytime I see that I know there has to be a better way.

 

Thanks for any suggestions

Wanna-Be C# Superstar
Posted
Can anybody point to some resources about building my own error handling routines?

What I have is a long process that involves calling several methods. During each method something could go wrong, in which case I want to avoid doing the remaining methods.

What I would like would be a centralized error handler that could flip a boolean to let me know if something went wrong. i would then have to test this boolean before each method.

Another reason I want to have this is because I see that I am writing the same 5 lines of code in each of my error handlers and anytime I see that I know there has to be a better way.

 

Thanks for any suggestions

 

Why not use Try/Catch/Throw/Finally...Link

-Sean
Posted

I am using that.

 

But when you call a method and that method calls another method

and all of your try catch blocks look like

 

try
....do something
catch ex as Exception
MessageBox
Alert
Console.WriteLine
finally
Conn.close
end try

 

 

You have a problem.

First of all you cannot easily change platforms from say an asp.net page to an asp.net web service (which I have had to do a few times). because one supports console.writeline and the other does not.

If this code were centralized then the problem goes away with one fix.

 

Incidentally, it seems to me that the most reliable way to write out error information across all platforms is the text file.

 

I drop this in all my code now

 

   Private Sub ToText(ByVal TextNote As String)
       If Not CType(AppSettings("Log"), Boolean) Then Exit Sub
       Dim path As String
       Dim fi As FileInfo
       Dim sw As StreamWriter

       Try
           path = AppSettings("LogFilePath")
           fi = New FileInfo(path)
           sw = fi.AppendText
           sw.WriteLine(TextNote)
           sw.Flush()
           sw.Close()
       Catch ex As Exception
       Finally
           sw = Nothing
       End Try

   End Sub

 

 

Basically what I am looking for is a more organized approach to the error handling where I can centralize my error reporting code and get information about where and when the error took place and what led up to it. I could also put email code in there to.

Wanna-Be C# Superstar
Posted

Sweet.

 

Thank you.

 

I've gotten to a point in programming where I can tell that something i'm doing can be done a better way, I just can't figure out the way most times. :rolleyes:

Wanna-Be C# Superstar

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...