Solved

Error Handling with C#

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

739 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