Solved

Try, Catch not Working c#

Posted on 2013-01-27
3
745 Views
Last Modified: 2013-01-27
Displays error message to user from the try block.
Does not execute the catch block.


What should I change?

        try
        {
            strResearchSiteSQL = "SELECT ResearchSiteID FROM PerfCriteria WHERE UserID = '" + strUserID + "'";
            string strResearchSitePref = GetColValue(conStockSelect, strResearchSiteSQL, "ResearchSiteID");
            int intResearchSitePref = int.Parse(strResearchSitePref);  // Error here
        }

//Displays error message to user.
//Does not execute the catch block
        catch (SmtpException err)
        {
            Session["StackTrace"] = err.StackTrace;
            Session["Message"] = err.Message;
            Session["Data"] = err.Data.ToString();
            Session["Source"] = err.Source;
            Session["ErrProcName"] = "cmdSend_Click";
            Response.Redirect("~/ErrorHandling/ShowError.aspx");
        }
0
Comment
Question by:Dovberman
3 Comments
 
LVL 42

Assisted Solution

by:sedgwick
sedgwick earned 150 total points
ID: 38823641
it's different exception being thrown, not smtpexception.

use the following code:
        try
        {
            strResearchSiteSQL = "SELECT ResearchSiteID FROM PerfCriteria WHERE UserID = '" + strUserID + "'";
            string strResearchSitePref = GetColValue(conStockSelect, strResearchSiteSQL, "ResearchSiteID");

            int intResearchSitePref;
if(!int.TryParse(strResearchSitePref, out intResearchSitePref)){
Session["ErrProcName"] = "cmdSend_Click";

string errMsg;

if(strResearchSitePref == null){
errMsg = string.Format("Invalid intResearchSitePref value is Null");
}else{
errMsg = string.Format("Invalid intResearchSitePref value: {0}", strResearchSitePref);
}

Session["Message"] = errMsg;
Response.Redirect("~/ErrorHandling/ShowError.aspx");

}
        }

//Displays error message to user.
//Does not execute the catch block
        catch (Exception err)
        {
            RedirectErrorPage(err, cmdSend_Click);
        }

void RedirectErrorPage(Exception err, string cmd){
Session["StackTrace"] = err.StackTrace;
            Session["Message"] = err.Message;
            Session["Data"] = err.Data.ToString();
            Session["Source"] = err.Source;
            Session["ErrProcName"] = cmd;
            Response.Redirect("~/ErrorHandling/ShowError.aspx");
}

Open in new window

0
 
LVL 6

Accepted Solution

by:
esolve earned 150 total points
ID: 38823684
It is important to position catch blocks with the most specific (that is, the most derived) exception types first.

http://msdn.microsoft.com/en-us/library/vstudio/ms173162.aspx

So in your scenario I will have two catch blocks

try
{
}
catch (SmtpException err)
{
  //Handle any specific errors
}
catch (System.Exception ex)
{
    //Handle any other uncaught exceptions.
    //In your case Int.Parse
}
0
 

Author Closing Comment

by:Dovberman
ID: 38823755
Thank you
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question