【发布时间】:2014-02-26 04:20:55
【问题描述】:
我是一名新的 ASP.net 开发人员,我正在尝试在我的简单 3 层基于 Web 的应用程序中添加适当的异常处理。通过关注this post,我在数据访问层 (DAL) 和用户界面 (UI) 中完成了以下操作:
DAL:
public IEnumerable<Survey> getData()
{
List<Survey> surveysList = new List<Survey>();
try
{
using (ItemsDBEntities context = new ItemsDBEntities())
{
surveysList = (from survey in context.Surveys
select new Survey()
{
ID = survey.ID,
startDate = survey.StartDate,
EndDate = survey.EndDate,
Description = survey.Description
}).ToList();
}
}
catch (EntityException ex)
{
//something wrong about entity
throw new ConnectionFailedException(ex);
}
catch (Exception ex)
{
//Don't know what happend...
}
return surveysList;
}
用户界面代码隐藏:
private void bindGrid()
{
Survey survey = new Survey();
try
{
GridView1.DataSource = survey.getData();
GridView1.DataBind();
GridView1.Visible = true;
}
catch (ConnectionFailedException)
{
Label1.Text = "There was a problem accessing the database, please try again.";
}
}
但是,我仍然在
下看到一条红线连接失败异常
在每一层,我不知道为什么,它给了我以下错误:
类型或命名空间名称“ConnectionFailedException”不能是 找到(您是否缺少 using 指令或程序集 参考?)TestWebsite\App_Code\DAL\Survey.cs
我该如何解决这个问题?我不想为每个异常类型创建一个类,我将像目前所做的那样抛出它。 如果可能的话,您能否给我一个帮助和示例?
【问题讨论】:
-
您的项目中是否引用了包含
ConnectionFailedException的程序集?如果是这样,您是否有适当的using指令?另外,关于异常的注释:不要捕获异常并且什么都不做(例如,一个空的 catch 块)。如果你不能处理异常,让它冒泡,直到 can 处理它的东西捕获它。
标签: c# asp.net exception-handling 3-tier