Properly Handling Exception Handling


Author
Message
Bill Cunnien
Bill Cunnien
StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)
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


Replies
Bill Cunnien
Bill Cunnien
StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)
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
Greg McGuffey
Strategic Support Team Member (4.8K reputation)
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
Bill Cunnien
StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)
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
Greg McGuffey
Strategic Support Team Member (4.8K reputation)
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.
Greg McGuffey
Greg McGuffey
Strategic Support Team Member (4.8K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
Oh, how did you send the email? I've been thinking about doing this also, and am investigating ways of doing it.
Trent Taylor
Trent Taylor
StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 7K
.NET actually has some good SMTP classes for sending emails.  Here is a simple example (I went ahead and showed how to authenticate as this is generally the next question.  If you are mailing from an internal server that will relay your message you might not need this, but I thought it would help.  Also, you can format the mail message to support HTML, etc.  I just showed you the basics.

Dim smtp As New System.Net.Mail.SmtpClient("mymail.server.com")
Dim mailItem As New System.Net.Mail.MailMessage("myEmail@somewhere.com", _
                                                "someone@there.com", _
                                                "Howdy", _
                                                "Here is the body")

smtp.Credentials = New System.Net.NetworkCredential("MyUserName", "MyPassword")
smtp.Send(mailItem)

Greg McGuffey
Greg McGuffey
Strategic Support Team Member (4.8K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
Thanks Trent. I've heard that this can fail because ISPs block mail sent to servers outside of their domain. Is this something you've heard of? I.e. I'm accessing the internet via a wireless connection at a hotel or using the network connection at a client site, attempt to send an email via the SMTP server at my company and the hosting network blocks the email.
Trent Taylor
Trent Taylor
StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 7K
Is this something you've heard of?

Thus the credentials.  If you supply a mail server, port, and credentials...that's email...period Smile  We use this everywhere.  The only thing that you cannot do is pull mail down from a POP3 server...but sending...no problem Smile

Greg McGuffey
Greg McGuffey
Strategic Support Team Member (4.8K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
Very cool! Thanks!
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Threaded View
Threaded View
Bill Cunnien - 17 Years Ago
Greg McGuffey - 17 Years Ago
             Well said, Greg ;)
Trent L. Taylor - 17 Years Ago
Bill Cunnien - 17 Years Ago
Greg McGuffey - 17 Years Ago
Bill Cunnien - 17 Years Ago
                         #1 is solved by properly referencing the namespace. That happens way...
Bill Cunnien - 17 Years Ago
                             I'd open the form as a dialog (ShowDialog method), then access...
Greg McGuffey - 17 Years Ago
                                 Yup...I think we met the same conclusion at about the same time. Here...
Bill Cunnien - 17 Years Ago
                                     Cool!

One thing to note is that I used the using statement....
Greg McGuffey - 17 Years Ago
                                         Oh, how did you send the email? I've been thinking about doing this...
Greg McGuffey - 17 Years Ago
                                             .NET actually has some good SMTP classes for sending emails. Here is a...
Trent L. Taylor - 17 Years Ago
                                                 Thanks Trent. I've heard that this can fail because ISPs block mail...
Greg McGuffey - 17 Years Ago
                                                     [quote]Is this something you've heard of? [/quote] Thus the...
Trent L. Taylor - 17 Years Ago
                                                         Very cool! Thanks!
Greg McGuffey - 17 Years Ago

Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search