Solved

Try, Catch not Working c#

Posted on 2013-01-27
3
733 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

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…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

786 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