Solved

Error Handling with C#

Posted on 2011-03-18
2
271 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
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

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

815 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