Solved

Error Handling with C#

Posted on 2011-03-18
2
273 Views
Last Modified: 2012-05-11
Hello,

I would like to handle an error that is thrown in Code that I am trying to capture in C#.  Basically I am testing a class.  In this class, I have the following:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;




/// <summary>
/// Summary description for MassEmail
/// </summary>
///
namespace MarketingEmail
{
    public class MassEmail
    {
        public string EmailName { get; set; }
        public string SubjectLine { get; set; }
        private string fromlabel;
        private string businessname;    


        public string FromLabel
        {
            get
            {
                return fromlabel;
            }
            set
            {
                if (value.Length < 1)
                {
                    throw new Exception("Invalid From Email Label");
                }                
                fromlabel = value;
            }
        }

        public string BusinessName
        {
            get
            {
                return businessname;
            }
            set
            {
                if (value.Length < 1)
                {
                    throw new Exception("Invalid Business Name");
                }
               
                businessname = value;
            }
        }

     


    }
}

And in my Default.aspx.cs page, I have the following code I am trying to capture the error with so that I can deal with it.  I know my error code is wrong, so how do I write the correct code?


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MarketingEmail;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        MassEmail emailblast;
        emailblast = new MassEmail();
        string emailcountry;
        emailcountry = "";
        emailblast.BusinessCountry = emailcountry;
        Response.Write(emailblast.BusinessCountry);
        
        if Error
        {
            Response.Write("Need a valid Country");
        }
        else
        {
            Response.Write(emailblast.BusinessCountry);
        }

        
        

    }
}

Open in new window

0
Comment
Question by:VBBRett
[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
2 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 250 total points
ID: 35166133
Perhaps something like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MarketingEmail;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
    	try
    	{
		MassEmail emailblast;
		emailblast = new MassEmail();
		string emailcountry;
		emailcountry = "";
		emailblast.BusinessCountry = emailcountry;
		Response.Write(emailblast.BusinessCountry);
	}
        catch(Exception ex)
        {
	    Response.Write("Need a valid Country");
	}      
    }
}

Open in new window

0
 
LVL 10

Assisted Solution

by:ALaRiva
ALaRiva earned 250 total points
ID: 35166154
You need to wrap the line for setting the value in a try/catch.

Something like this:

try {
   emailblast.BusinessCountry = emailcountry;
}
catch (Exception e){            
   Response.Write "Need a Valid Country"            
}
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

735 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