我把代码重新排版一下:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
……
#region Initialize Method
public pub() {
  this.cn = new SqlConnection();
  this.cn.ConnectionString = "data source=\"NORSSION-SERVER\";user id=sa;password=c00202";
  try {
    cn.Open();
  }
  catch( Exception ex ) {
    string s = ex.GetType().FullName;
    System.Diagnostics.Debug.WriteLine( s );
    switch( s ) {
      case "System.Data.SqlClient.SqlException":
        //show some message
        //我想在这里写抛出的异常消息,怎么写?用Response.Write不行
        break;
      case "System.InvalidCastException":
      case "System.NullReferenceException":
      case "System.Exception":
        //show some message
        //我想在这里写抛出的异常消息,怎么写?用Response.Write不行
        break;
      default:
        //Unknown class of exception
        //show some message
        //我想在这里写抛出的异常消息,怎么写?用Response.Write不行
        break;
    }
}
}
#endregion

解决方案 »

  1.   

    如果把捕获的异常直接抛出,则:
    throw ex;如果要抛出一个新的异常:throw new ApplicationException("异常信息");或者:throw new 自己的异常类();
      

  2.   

    这种抛出异常的方式并不友好,而且也不是像我说的那样,能够出现一个像JavaScript的alert()所弹出的窗体,或者出现类似于在VC下弹出的MessageBox()窗体。其实简单点来说,就是要求能够Response.Write一段<script language=JavaScript>alert("faint")</script>代码,但是直接写Response.Write不行(在系统自动生成的.aspx.cs文件中是可以的,我试着在.cs文件中using了System.Web也不行)。
      

  3.   


    HttpContext.Current.Response.Write("<script>alert('Hello');</script>");
      

  4.   

    同意timmy3310(Tim) 
    抛出异常,只是说明程序有异常发生!如要的到你的结果:
    try
    .....
    catch execption e
      this.registerstartupscript("err1","<script language='javascript'>alert('faint');</script>")
    finally
    ...
    end try
      

  5.   

    用timmy3310(Tim)的方法确实可行。
    LineCorner(),你说的方法我也看到过,但是使用后并没有任何效果啊。我的代码如下:
    using System.Web.UI;
    ……
          Page p = new Page();
          p.RegisterStartupScript("err1", "<script>alert('Hello');</script>");
    ……
    应该怎么改?
      

  6.   

    Page p = (Page)HttpContext.Current.Handler;
    p.RegisterStartupScript("err1", "<script>alert('Hello');</script>");