Solved

Error Handling with C#

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

15 Experts available now in Live!

Get 1:1 Help Now