Bill Cunnien
|
|
Group: Forum Members
Posts: 785,
Visits: 3.6K
|
Good morning, I am working through some intricate code and I want to make sure I am made aware of any errors that may pop up at run-time. How I have done this in the past is to create a class that I throw exceptions at and from there send emails or alerts to let me know what is going on. As I understand it there is some exception handling that is already going on within the framework via an error provider. How do I take advantage of that and still provide a streamlined way of handling my exceptions? Also, is this a proper approach to using transactions within my code? Here is an example of my code that I am trying to work this through: private void cmdTransfers_Click(object sender, EventArgs e) { try { // Wrap this entire process in a transaction BusinessLayer.TransactionBegin("MyDataSourceName", IsolationLevel.ReadCommitted);
// Retrieve the next [snip] . . . lots of stuff happening here . . .
MyLastProcedureToRun(myParm);
BusinessLayer.TransactionCommit("MyDataSourceName"); } catch (Exception ex) { // place cool SF exception handling here, if any
// this is my current way of doing it SPXExceptionHandler.ReportException(ex); // exceptions are logged, emailed, etc. BusinessLayer.TransactionRollback("Dynamics"); } }
Thanks! Bill
|
|
|
Greg McGuffey
|
|
Group: Forum Members
Posts: 2K,
Visits: 6.6K
|
Bill, It is actually way easier than this. There is an application event handler in program.cs (appmain.vb) that is for unhandled exceptions. You'd put your SPXExceptionHandler.ReportException(ex); call in there and indicate through the event hards that you've handled it. Now any unhandled exception is handled by this code...every where. Now in the case of using a try/catch, so you can do the rollback, you'd still want to handle that, but after you do the rollback, you'd rethrow the exception. You could even wrap the original exception in one specific to that situation. private void cmdTransfers_Click(object sender, EventArgs e)
{
try
{
// Wrap this entire process in a transaction
BusinessLayer.TransactionBegin("MyDataSourceName", IsolationLevel.ReadCommitted);
// Retrieve the next [snip] . . . lots of stuff happening here . . .
MyLastProcedureToRun(myParm);
BusinessLayer.TransactionCommit("MyDataSourceName");
}
catch (Exception ex)
{
BusinessLayer.TransactionRollback("Dynamics");
// rethrow the original exception now that rollback is complete
Throw
}
} Note that the procedure is only responsible for throwing the exception, not dealing with it...that is done in the application unhandled exception handler! Hope that makes sense!
|
|
|
Trent Taylor
|
|
Group: StrataFrame Developers
Posts: 6.6K,
Visits: 6.9K
|
Well said, Greg
|
|
|
Bill Cunnien
|
|
Group: Forum Members
Posts: 785,
Visits: 3.6K
|
Thanks, Greg! So, placing a throw after the rollback will force the handled exception to be an unhandled exception which will actually be handled by the program.cs unhandled exception handler? Right?
|
|
|
Greg McGuffey
|
|
Group: Forum Members
Posts: 2K,
Visits: 6.6K
|
Well, when you say it like that, it is all so clear now But it is exactly right!
|
|
|
Bill Cunnien
|
|
Group: Forum Members
Posts: 785,
Visits: 3.6K
|
Ok...I am now attempting to catch all unhandled exception in the program.cs. What I want to do is popup a nice message telling the user that a bad thing has just happened and that the IT department has been sent an email. This works. The email gets delivered. In addition to the nice message, I want to open a dialog window that will allow the user to enter any information that they would like to help explain what they were doing at the time of the error. Once filled in, the dialog closes and adds that info to the body of the email before sending. I am having two problems: 1) The form that I created for doing this is not accessible by the code in program.cs. 2) The string variable cannot be fed back to the program.cs routine. Thought? Bill
|
|
|
Bill Cunnien
|
|
Group: Forum Members
Posts: 785,
Visits: 3.6K
|
#1 is solved by properly referencing the namespace. That happens way too often for comfort . . . bah! foiled again! For the moment, the second need still eludes me. Curse you, Aqua-Scum! Bill
|
|
|
Greg McGuffey
|
|
Group: Forum Members
Posts: 2K,
Visits: 6.6K
|
I'd open the form as a dialog (ShowDialog method), then access properties on the form to get the info from the form. Code would be something like this (my C# is a bit rusty...hopefully this gets the idea across): // UserErrorDialog is form that collects info from user
String userInfo = String.Empty;
using ( UserErrorDialog form = new UserErrorDialog())
{
// Show the form as a dialog, which will wait for user to close the form
if (form.ShowDialog()== DialogReslut.OK)
{
// Get user info from properties on form
userInfo = form.UserInfo
}
}
// Send email, display dialog whatever... Hope that helps!
|
|
|
Bill Cunnien
|
|
Group: Forum Members
Posts: 785,
Visits: 3.6K
|
Yup...I think we met the same conclusion at about the same time. Here is what I did: Set a public variable in the dialog form (Form2): public String _userinfo; On the cmdOK_Click event, I set the value of the comments textedit control to the public variable. Then, on Form1, within the deep inner workings of the unhandled exception routine: MyExceptionData mNewForm = new MyExceptionData(e.UnhandledException); mNewForm.ShowDialog(); mUserData = mNewForm._userinfo;I am passing the exception to the form because I am formatting the exception data on the form for my own (or other savvy users') review. So far, so good...thanks a ton for your help!! Bill
|
|
|
Greg McGuffey
|
|
Group: Forum Members
Posts: 2K,
Visits: 6.6K
|
Cool!
One thing to note is that I used the using statement. It closes the form when done (when the using code block is done). If you don't use this, then you should close the form after you are done with it. The form is still open otherwise.
|
|
|