﻿<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>StrataFrame Forum » StrataFrame Application Framework - V1 » WinForms (How do I?)  » Properly Handling Exception Handling</title><generator>InstantForum 2017-1 Final</generator><description>StrataFrame Forum</description><link>http://forum.strataframe.net/</link><webMaster>StrataFrame Forum</webMaster><lastBuildDate>Sun, 03 May 2026 10:24:13 GMT</lastBuildDate><ttl>20</ttl><item><title>Properly Handling Exception Handling</title><link>http://forum.strataframe.net/FindPost16825.aspx</link><description>Good morning,&lt;/P&gt;&lt;P&gt;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.&amp;nbsp; 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.&amp;nbsp; As I understand it there is some exception handling that is already going on within the framework via an error provider.&amp;nbsp; How do I take advantage of that and still provide a streamlined way of handling my exceptions?&amp;nbsp; Also, is this a proper approach to using transactions within my code?&lt;/P&gt;&lt;P&gt;Here is an example of my code that I am trying to work this through:&lt;/P&gt;&lt;FONT size=2&gt;&lt;P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;private&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;void&lt;/FONT&gt;&lt;FONT size=2&gt; cmdTransfers_Click(&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;object&lt;/FONT&gt;&lt;FONT size=2&gt; sender, &lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;EventArgs&lt;/FONT&gt;&lt;FONT size=2&gt; e)&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;try&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT size=2&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#008000 size=2&gt;// Wrap this entire process in a transaction&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;BusinessLayer&lt;/FONT&gt;&lt;FONT size=2&gt;.TransactionBegin(&lt;/FONT&gt;&lt;FONT color=#a31515 size=2&gt;"MyDataSourceName"&lt;/FONT&gt;&lt;FONT size=2&gt;, &lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;IsolationLevel&lt;/FONT&gt;&lt;FONT size=2&gt;.ReadCommitted);&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#008000 size=2&gt;// Retrieve the next [snip] . . . lots of stuff happening here . . .&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT size=2&gt;MyLastProcedureToRun(myParm);&lt;BR&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;BusinessLayer&lt;/FONT&gt;&lt;FONT size=2&gt;.TransactionCommit(&lt;/FONT&gt;&lt;FONT color=#a31515 size=2&gt;"MyDataSourceName"&lt;/FONT&gt;&lt;FONT size=2&gt;);&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;catch&lt;/FONT&gt;&lt;FONT size=2&gt; (&lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;Exception&lt;/FONT&gt;&lt;FONT size=2&gt; ex)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#008000 size=2&gt;// place cool SF exception handling here, if any&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#008000 size=2&gt;// this is my current way of doing it&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT size=2&gt;SPXExceptionHandler.ReportException(ex);&amp;nbsp; &lt;FONT color=#008000&gt;//&amp;nbsp;exceptions are logged, emailed, etc.&lt;/FONT&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;&lt;FONT color=#4b6e9d&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;BusinessLayer&lt;/FONT&gt;&lt;FONT size=2&gt;.TransactionRollback(&lt;/FONT&gt;&lt;FONT color=#a31515 size=2&gt;"Dynamics"&lt;/FONT&gt;&lt;FONT size=2&gt;);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;}&lt;BR&gt;&lt;BR&gt;Thanks!&lt;BR&gt;Bill&lt;/P&gt;&lt;/FONT&gt;</description><pubDate>Fri, 06 Jun 2008 16:06:53 GMT</pubDate><dc:creator>Bill Cunnien</dc:creator></item><item><title>RE: Properly Handling Exception Handling</title><link>http://forum.strataframe.net/FindPost16894.aspx</link><description>Very cool! Thanks!</description><pubDate>Fri, 06 Jun 2008 16:06:53 GMT</pubDate><dc:creator>Greg McGuffey</dc:creator></item><item><title>RE: Properly Handling Exception Handling</title><link>http://forum.strataframe.net/FindPost16888.aspx</link><description>[quote]Is this something you've heard of? [/quote]&lt;/P&gt;&lt;P&gt;Thus the credentials.&amp;nbsp; If you supply a mail server, port, and credentials...that's email...period :)&amp;nbsp; We use this everywhere.&amp;nbsp; The only thing that you cannot do is pull mail down from a POP3 server...but sending...no problem :)</description><pubDate>Fri, 06 Jun 2008 14:18:31 GMT</pubDate><dc:creator>Trent L. Taylor</dc:creator></item><item><title>RE: Properly Handling Exception Handling</title><link>http://forum.strataframe.net/FindPost16885.aspx</link><description>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.</description><pubDate>Fri, 06 Jun 2008 11:44:05 GMT</pubDate><dc:creator>Greg McGuffey</dc:creator></item><item><title>RE: Properly Handling Exception Handling</title><link>http://forum.strataframe.net/FindPost16882.aspx</link><description>.NET actually has some good SMTP classes for sending emails.&amp;nbsp; Here is a simple example (I went ahead and showed how to authenticate as this is generally the next question.&amp;nbsp; If you are mailing from an internal server that will relay your message you might not need this, but I thought it would help.&amp;nbsp; Also, you can format the mail message to support HTML, etc.&amp;nbsp; I just showed you the basics.&lt;/P&gt;&lt;P&gt;[codesnippet]Dim smtp As New System.Net.Mail.SmtpClient("mymail.server.com")&lt;BR&gt;Dim mailItem As New System.Net.Mail.MailMessage("myEmail@somewhere.com", _&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "someone@there.com", _&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "Howdy", _&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "Here is the body")&lt;BR&gt;&lt;BR&gt;smtp.Credentials = New System.Net.NetworkCredential("MyUserName", "MyPassword")&lt;BR&gt;smtp.Send(mailItem)[/codesnippet]</description><pubDate>Fri, 06 Jun 2008 08:42:08 GMT</pubDate><dc:creator>Trent L. Taylor</dc:creator></item><item><title>RE: Properly Handling Exception Handling</title><link>http://forum.strataframe.net/FindPost16862.aspx</link><description>Oh, how did you send the email?  I've been thinking about doing this also, and am investigating ways of doing it.</description><pubDate>Thu, 05 Jun 2008 13:01:19 GMT</pubDate><dc:creator>Greg McGuffey</dc:creator></item><item><title>RE: Properly Handling Exception Handling</title><link>http://forum.strataframe.net/FindPost16861.aspx</link><description>Cool!&lt;br&gt;
&lt;br&gt;
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.</description><pubDate>Thu, 05 Jun 2008 13:00:17 GMT</pubDate><dc:creator>Greg McGuffey</dc:creator></item><item><title>RE: Properly Handling Exception Handling</title><link>http://forum.strataframe.net/FindPost16860.aspx</link><description>Yup...I think we met the same conclusion at about the same time.&amp;nbsp; Here is what I did:&lt;/P&gt;&lt;P&gt;Set a public variable in the dialog form (Form2):&lt;/P&gt;&lt;P&gt;public String _userinfo;&lt;/P&gt;&lt;P&gt;On the cmdOK_Click event, I set the value of the comments textedit control to the public variable.&lt;/P&gt;&lt;P&gt;Then, on Form1, within the deep inner workings of the unhandled exception routine:&lt;/P&gt;&lt;FONT size=2&gt;&lt;P&gt;&lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;MyExceptionData&lt;/FONT&gt;&lt;FONT size=2&gt; mNewForm = &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;new&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;MyExceptionData&lt;/FONT&gt;&lt;FONT size=2&gt;(e.UnhandledException);&lt;BR&gt;mNewForm.ShowDialog();&lt;BR&gt;mUserData = mNewForm._userinfo;&lt;/P&gt;&lt;/FONT&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;So far, so good...thanks a ton for your help!!&lt;BR&gt;Bill</description><pubDate>Thu, 05 Jun 2008 12:56:05 GMT</pubDate><dc:creator>Bill Cunnien</dc:creator></item><item><title>RE: Properly Handling Exception Handling</title><link>http://forum.strataframe.net/FindPost16859.aspx</link><description>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):&lt;br&gt;
[codesnippet]// UserErrorDialog is form that collects info from user&lt;br&gt;
String userInfo = String.Empty;&lt;br&gt;
using ( UserErrorDialog form = new UserErrorDialog()) &lt;br&gt;
{&lt;br&gt;
  // Show the form as a dialog, which will wait for user to close the form&lt;br&gt;
  if (form.ShowDialog()== DialogReslut.OK)&lt;br&gt;
  {&lt;br&gt;
    // Get user info from properties on form&lt;br&gt;
    userInfo = form.UserInfo&lt;br&gt;
  }&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
// Send email, display dialog whatever...[/codesnippet]&lt;br&gt;
&lt;br&gt;
Hope that helps!</description><pubDate>Thu, 05 Jun 2008 12:44:19 GMT</pubDate><dc:creator>Greg McGuffey</dc:creator></item><item><title>RE: Properly Handling Exception Handling</title><link>http://forum.strataframe.net/FindPost16858.aspx</link><description>#1 is solved by properly referencing the namespace.&lt;/P&gt;&lt;P&gt;That happens way too often for comfort . . . bah!&amp;nbsp; foiled again!&lt;/P&gt;&lt;P&gt;For the moment, the second need still eludes me.&amp;nbsp; &lt;/P&gt;&lt;P&gt;Curse you, Aqua-Scum!&lt;BR&gt;Bill</description><pubDate>Thu, 05 Jun 2008 12:23:41 GMT</pubDate><dc:creator>Bill Cunnien</dc:creator></item><item><title>RE: Properly Handling Exception Handling</title><link>http://forum.strataframe.net/FindPost16857.aspx</link><description>Ok...I am now attempting to catch all unhandled exception in the program.cs.&amp;nbsp; What&amp;nbsp;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.&amp;nbsp; This works.&amp;nbsp; The email gets delivered.&amp;nbsp; 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.&amp;nbsp; Once filled in, the dialog closes and adds that info to the body of the email before sending.&lt;/P&gt;&lt;P&gt;I am having two problems:&lt;/P&gt;&lt;P&gt;1)&amp;nbsp; The form that I created for doing this is not accessible by the code in program.cs.&lt;/P&gt;&lt;P&gt;2)&amp;nbsp; The string variable cannot be fed back to the program.cs routine.&lt;/P&gt;&lt;P&gt;Thought?&lt;BR&gt;Bill</description><pubDate>Thu, 05 Jun 2008 12:17:22 GMT</pubDate><dc:creator>Bill Cunnien</dc:creator></item><item><title>RE: Properly Handling Exception Handling</title><link>http://forum.strataframe.net/FindPost16844.aspx</link><description>Well, when you say it like that, it is all so clear now  :blink:&lt;br&gt;
&lt;br&gt;
But it is exactly right!  :cool:</description><pubDate>Wed, 04 Jun 2008 16:08:11 GMT</pubDate><dc:creator>Greg McGuffey</dc:creator></item><item><title>RE: Properly Handling Exception Handling</title><link>http://forum.strataframe.net/FindPost16841.aspx</link><description>Thanks, Greg!&lt;/P&gt;&lt;P&gt;So, placing a throw after the rollback will force the handled exception to be&amp;nbsp;an unhandled exception which will actually be handled by the program.cs unhandled exception handler?&lt;/P&gt;&lt;P&gt;Right?&lt;/P&gt;&lt;P&gt;:smooooth:</description><pubDate>Wed, 04 Jun 2008 15:31:02 GMT</pubDate><dc:creator>Bill Cunnien</dc:creator></item><item><title>RE: Properly Handling Exception Handling</title><link>http://forum.strataframe.net/FindPost16838.aspx</link><description>Well said, Greg ;)</description><pubDate>Wed, 04 Jun 2008 13:22:28 GMT</pubDate><dc:creator>Trent L. Taylor</dc:creator></item><item><title>RE: Properly Handling Exception Handling</title><link>http://forum.strataframe.net/FindPost16831.aspx</link><description>Bill,&lt;br&gt;
&lt;br&gt;
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.&lt;br&gt;
&lt;br&gt;
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.&lt;br&gt;
[codesnippet]private void cmdTransfers_Click(object sender, EventArgs e)&lt;br&gt;
{&lt;br&gt;
    try&lt;br&gt;
    {&lt;br&gt;
        // Wrap this entire process in a transaction&lt;br&gt;
        BusinessLayer.TransactionBegin("MyDataSourceName", IsolationLevel.ReadCommitted);&lt;br&gt;
&lt;br&gt;
        // Retrieve the next [snip] . . . lots of stuff happening here . . .&lt;br&gt;
&lt;br&gt;
       MyLastProcedureToRun(myParm);&lt;br&gt;
&lt;br&gt;
        BusinessLayer.TransactionCommit("MyDataSourceName");&lt;br&gt;
    }&lt;br&gt;
    catch (Exception ex)&lt;br&gt;
    {&lt;br&gt;
         BusinessLayer.TransactionRollback("Dynamics");&lt;br&gt;
&lt;br&gt;
         // rethrow the original exception now that rollback is complete&lt;br&gt;
         Throw&lt;br&gt;
    }&lt;br&gt;
}[/codesnippet]&lt;br&gt;
Note that the procedure is only responsible for throwing the exception, not dealing with it...that is done in the application unhandled exception handler! &lt;br&gt;
&lt;br&gt;
Hope that makes sense!</description><pubDate>Wed, 04 Jun 2008 11:25:27 GMT</pubDate><dc:creator>Greg McGuffey</dc:creator></item></channel></rss>