Solved

Try, Catch not Working c#

Posted on 2013-01-27
3
768 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

763 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