Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Try, Catch not Working c#

Posted on 2013-01-27
3
Medium Priority
?
873 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 600 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 600 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

670 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