如何显示错误消息?

时间:2013-10-03 17:35:01

标签: c# asp.net

我在这里有一些查找图像的代码,但是如果图像不存在则应该显示一些错误文本。它只是显示损坏的图像,不使用错误文本。在调试时,我看到catch异常看到404错误,但它只是跳过异常响应文本。这是我的代码:

protected void Page_Load(object sender, EventArgs e)
{
    checkMember(true);

    string acctNumber = Request["acctNum"];
    if (member.findAcct(acctNumber, "CheckImage") != null)
    {
        try
        {
            Response.ContentType = "image/jpeg";
            Response.BinaryWrite(((Member)member).checkImage(acctNumber, Request["ckNum"],DateTime.Parse(Request["date"]), Request["amt"], Request["checkSide"]));
        }
        catch (Exception ex)
        {
            Response.ContentType = "text";
            Response.Write("Error retrieving check image: "+ ex.Message);
        }
    }
    Response.End();

它刚刚跳过:

catch (Exception ex)
{
    Response.ContentType = "text";
    Response.Write("Error retrieving check image: "+ ex.Message);
}

1 个答案:

答案 0 :(得分:1)

也许你没有得到异常,你的if语句只是返回false?添加一个else来处理错误...

string acctNumber = Request["acctNum"];
if (member.findAcct(acctNumber, "CheckImage") != null)
{
     try
     {
         Response.ContentType = "image/jpeg";
         Response.BinaryWrite(((Member)member).checkImage(acctNumber, Request["ckNum"],DateTime.Parse(Request["date"]), Request["amt"], Request["checkSide"]));
     }
     catch (Exception ex)
     {
         Response.ContentType = "text";
         Response.Write("Error retrieving check image: " + ex.Message);
     }
}
else
{
     Response.ContentType = "text";
     //Exception Message can´t be referenced here
     Response.Write("Error retrieving check image");
}