1

我有一个 try-catch 块:

try{
//stuff here
}catch(Exception x){
    tran.Rollback();
    Response.Write("<script>alert('Error details: " + x + "');</script>");
    Response.Write("<script>alert('Error');</script>");
}

将弹出“错误”消息,但不会弹出“错误详细信息”。我错过了什么?我试过 x.ToString(), x.Message...什么都没有...

4

3 回答 3

2

您可能想要序列化您放入该脚本的字符串。

try 
{

}
catch (Exception ex)
{
    JavaScriptSerializer ser = new JavaScriptSerializer();
    String jsSafeString = ser.Serialize(ex.Message);
    // use this string in your script.
}
于 2012-12-08T16:46:12.120 回答
0

尝试这个

cleanMessage是你的exception string

string script = string.Format("alert('{0}');", cleanMessage);
if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert")) 
{
    page.ClientScript.RegisterClientScriptBlock(page.GetType(), "alert", script, true /* addScriptTags */);
}
于 2012-12-08T17:18:30.153 回答
0

如果只希望在页面上打印消息,只需获取异常类的Message属性即可

try{
//stuff here
}catch(Exception x){
tran.Rollback();
Response.Write("<script>alert('Error details: " + x.Message + "');</script>");
Response.Write("<script>alert('Error');</script>");

}

于 2012-12-08T16:00:04.790 回答