[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Error Handling with C#

Posted on 2011-03-18
2
Medium Priority
?
279 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 1000 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 1000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

656 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