Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Try Catch question 1

Status
Not open for further replies.

NuJoizey

MIS
Aug 16, 2006
450
0
0
US
Can I do something like:

Code:
Protected Sub myDropdown_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
  
Try
    UpdateRemoteDB()
    SendAnAlertMailThatItWorked()
Catch ex As Exception
    DisplayMessageThatUpdateRemoteDBFailed()
    SendEmailToAdminWithErrorDetail()
End Try

End Sub

where in the case UpdateRemoteDB() fails, then DisplayMessageThatUpdateRemoteDBFailed() would ideally trigger a message that the remote db update failed and that the user should contact the admin.

However, i am trying to make it as unobtrusive as possible - i.e. i would rather not redirect the user to whole different custom error page: if possible I'd prefer to return an error message to the screen on some sort of asp.net control - ie: to perhaps trigger visibility of a hidden label, (or maybe a customvalidator control????) in a spot on the screen close to the dropdown.

So is it possible to trap the exception as it "bubbles up" through the stack and use it within your app on an asp.net control?
 
Once you are in the Catch, you have the exception object(ex). From there, you can do what you want with it. You can use VS help to see the properties on the various exception objects that you can display.
In your case, you may want 2 separate try .. catch statements. One for the DB call and one for the Email call.
 
ok. thanks. i was basically looking for validation of whether or not you could catch the error and cause it to display a message in the UI on a control. i gather that you can. i am working on making this happen now by having it display a simple message on a label. should be pretty simple - guess I really don't need the validator control, that would probably overcomplicate
 
FYI you can have multiple catches as well.
Catch for any sqlexceptions and another catch for all other errors ect..

or you could put a try catch around your connection and then another around the execution of your stored proc or sql statment. So then you could make your connection and then if error catch it or validate that you have an open connection ect..

Hope this helps also.

Ordinary Programmer
 
your code doesn't match your logic.
where in the case UpdateRemoteDB() fails, then DisplayMessageThatUpdateRemoteDBFailed()
but what if SendAnAlertMailThatItWorked() fails? that has nothing to do with the database. and if sending an email fails, there is a good chance it will fail in your catch block as well. then what?

i would replace the try/catch with if/else
Code:
if(the data is not valid)
{
    DisplayWarnings();
    return ;
}
UpdateRemoteDB();
SendAnAlertMailThatItWorked();
if an exception is thrown it will bubble up and be handled elsewhere. I also find the try/catch blocks distract from the true intent of what the member should be doing. most of the time a given member should not be concerned with how to handle an exception.

search for "Exception Handling Guidelines" or "Rules for Exception Handling" for more information on the topic.

finally, look into a logging framework rather than rolling your own. a logging framework will provide multiple output locations as well as internally manage exception thrown due to logging configuration errors. log4net is very popular. I believe the MS application blocks have one as well.



Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top