Solved

Try, Catch not Working c#

Posted on 2013-01-27
3
696 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now