ASP.NET事务和异常处理

1.1什么是事务处理?
• 事务是一组组合成逻辑工作单元的数据库
操作,虽然系统中可能会出错,但事务将
控制和维护每个数据库的一致性和完整性。
• 如果在事务过程中没有遇到错误,事务中
的所有修改都将永久成为数据库的一部分。
• 如果遇到错误,则不会对数据库作出任何
修改。
1.2事务处理过程
1. 开始一个事务。进入“事务待命”状态。
2. 在“事务待命”状态,记录事务中改变的数据库记录。此
改变不能直接改变数据库中的值,必须先用一个顺序
的“事务日志”记录在一边。同时,对于要改变的原始记
录加锁,让其它用户无法读和写。如果记录已经被其
它事务加锁,则报错。
3. 在“事务待命”,如果用户给出commit transaction命
令,则进入“事务拷贝”状态,拷贝所有加锁的记录成备
份。
4. 上面3执行完,则进入“事务更新”状态,用“事务日志”中
记录一一更新实际的数据库记录。
5. 上面4执行完,则进入“事务结束”状态,释放所有的记
录锁,然后抛弃“事务日志”和备份的原数据库记录。
6. 上面5做完后,事务被删除
事务处理过程
但是,最为关键的是,事务系统必须执行
以下过程:一但数据库由于软件、硬件问
题发生故障,重启动后,一旦有事务没正
常删除,则:
– 7. 如果在“事务待命”、“事务结束”状态,则重新
从5中结束事务的动作开始执行。
– 8. 如果在“事务更新”状态,则重新从4开始更新
记录,并继续想下执行。结果,虽然系统崩溃
过,但事务仍然能正常提交。
1.4事务处理有关事项
• 事务处理的关键是在提交事务或者取消事务时,
万一系统崩溃了,数据库在再次启动时,仍然需
要保持数据可逻辑一致性。
• 应用中包含的事务应当尽量让它“瞬间”完成,避
免在比较忙时造成用户进程的互锁。
• Informix、Oracle、DB2等数据库的实际事务处理
流程更复杂,目的是具有更好的抵抗系统错误性
质,因为事务保护是业务系统安全稳定的最后一
道防线。
事务处理方法
• 直接写入SQL
• 通过ADO.NET实现
• COM+事务(分布式事务)
2.1直接写入SQL
• 在存储过程中使用BEGIN TRAN,
COMMIT TRAN, ROLLBACK TRAN实现
• 优点:
– 所有事务逻辑包含在一个单独的调用中
– 拥有运行一个事务的最佳性能
– 独立于应用程序
• 限制:
– 事务上下文仅存在于数据库调用中
– 数据库代码与数据库系统有关
CREATE PROCEDURE [SqlAction] AS
begin TRAN
declare @UserInfoError int
delete from [tbUserInfo] where username='邵志东'
select @UserInfoError =@@error
if(@UserInfoError =0)
COMMIT TRAN
else
ROLLBACK TRAN
GO

private void btnOK_Click(object sender, System.EventArgs e)
        
{
            SqlConnection myConnection 
= new SqlConnection("Server=(local);database=myDataBase;UID=sa;pwd=111;");
            myConnection.Open();
            SqlCommand myCommand 
= new SqlCommand();
            myCommand.Connection
=myConnection;
            myCommand.CommandType 
= CommandType.StoredProcedure;
            myCommand.CommandText 
= "SqlAction";
            myCommand.ExecuteNonQuery();
            myConnection.Close();
        }


2.2ADO.NET事务处理
在ADO.NET 中,可以使用Connection 和
Transaction 对象来控制事务。若要执行事务,请
执行下列操作:
• 调用Connection 对象的BeginTransaction 方法
来标记事务的开始。
• 将Transaction 对象分配给要执行的Command
的Transaction 属性。
• 执行所需的命令。
• 调用Transaction 对象的Commit 方法来完成事
务,或调用Rollback 方法来取消事务。


using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace ASPNETAction
{
    
/// <summary>
    
/// WebForm1 的摘要说明。
    
/// </summary>

    public class WebForm1 : System.Web.UI.Page
    
{
        
protected System.Web.UI.WebControls.Button btnOK;
    
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
// 在此处放置用户代码以初始化页面
        }


        
Web Form Designer generated code

        
private void btnOK_Click(object sender, System.EventArgs e)
        
{
            SqlConnection myConnection 
= new SqlConnection("Server=(local);Initial Catalog=Northwind;uid=sa;pwd=111;");
            myConnection.Open();
            
// 启动一个事务
            SqlTransaction myTrans = myConnection.BeginTransaction();


            
// 为事务创建一个命令
            SqlCommand myCommand = new SqlCommand();
            myCommand.Connection
=myConnection;
            myCommand.Transaction 
= myTrans;
            
try
            
{
                myCommand.CommandText 
= "Insert into Region (RegionID, RegionDescription) VALUES (110, 'Description')";
                myCommand.ExecuteNonQuery();
                
//myTrans.Commit();
                myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (111, 'Description')";
                myCommand.ExecuteNonQuery();
                myTrans.Commit();
                Response.Write(
"成功写入记录!");
            }

            
catch(Exception Ex)
            
{
                myTrans.Rollback();
                Response.Write(Ex.ToString());
                Response.Write(
"写入数据库失败!");
            }

            
finally
            
{
                myConnection.Close();
            }


        }

    }


ADO.NET事务处理
• 优点:
– 简单性
– 和数据据事务差不多的快
– 独立于数据库,不同数据库的专有代码被隐藏

• 缺点:
– 事务不能跨越多个数据库连接
– 事务执行在数据库连接层上,所以需要在事务
过程中维护一个数据库连接
2.3 COM+事务
一般的数据库事务控制要求事务里所做的
操作必须在同一个数据库内,这样在出现
错误的时候才能回滚(RllBack)到初始状
态。这就存在一个问题,在分布式应用程
序中,我们往往需要同时操作多个数据
库,使用数据库本身的事务处理,很难满
足程序对事务控制的要求。在COM+中,提
供了完整的事务服务,我们可以利用它来
完成在分布式应用程序中的事务控制。
创建参与自动事务的类
1. 将TransactionAttribute 类应用于您的类,指
定组件请求的自动事务类型。事务类型必须是
TransactionOption 枚举的成员。
2. 从ServicedComponent 类派生您的类。
ServicedComponent 是所有使用COM+ 服务
的类的基类。
3. 用强名称(strong name) 标记(sign) 程序集
(assembly),确保程序集包含唯一的密钥对。
4. 在COM+ catalog 中注册包含您的类的程序集。
• 定义一个COM+事务处理的类
[Transaction(TransactionOption.Required)]
public class
DataAccess:System.EnterpriseServices.ServicedCo
mponent
{
}
• TransactionOption枚举类型支持5个COM+值
– Disabled 忽略当前上下文中的任何事务。
– NotSupported 使用非受控事务在上下文中创建组件。
– Required 如果事务存在则共享事务,并且如有必要则创
建新事务。
– RequiresNew 使用新事务创建组件,而与当前上下文的状
态无关。
– Supported 如果事务存在,则共享该事务。
为使该组件正确运行,该组件必须有一个强名称。
生成一个强名称,然后使用该强名称对程序集进
行签名。为此,请按照下列步骤操作:
• 在命令提示符下,键入sn.exe -k
snEnterprise.snk 以创建一个密钥文件。
• 将snEnterprise.snk 复制到您的项目文件夹中。
• 在AssemblyInfo.vc 中,将以下代码添加到其他
程序集属性语句之前或之后:[assembly:
AssemblyKeyFileAttribute("..\\..\\snEnterprise.sn
k")]
• 进行保存,然后生成您的项目
• 使用regsvcs.exe将Dll注册到COM+ Services里面

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace ASPNETAction
{
    
/// <summary>
    
/// _03ComPlusAction 的摘要说明。
    
/// </summary>

    public class _03ComPlusAction : System.Web.UI.Page
    
{
        
protected System.Web.UI.WebControls.Button btnCommit1;
        
protected System.Web.UI.WebControls.Button btnCommit2;
    
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
// 在此处放置用户代码以初始化页面
        }


        
Web Form Designer generated code

        
private void btnCommit1_Click(object sender, System.EventArgs e)
        
{
            prjEnterprise.clsES myTest 
= new prjEnterprise.clsES();

            
try
            
{
                myTest.dbAccess(
1650);
                Response.Write(
"事务成功!");
            }

            
catch (Exception )
            
{
                Response.Write(
"事务失败!");
            }

        
        }


        
private void btnCommit2_Click(object sender, System.EventArgs e)
        
{
            prjEnterprise.clsES myTest 
= new prjEnterprise.clsES();

            
try
            
{
                myTest.dbAccess(
1100000000000);
                Response.Write(
"事务成功!");
            }

            
catch (Exception )
            
{
                Response.Write(
"事务失败!");
            }

        }

    }

}

using System.Reflection;
using System.Runtime.CompilerServices;

//
// 有关程序集的常规信息是通过下列 
//属性集控制的。更改这些属性值可修改与程序集
//关联的信息。
//
[assembly: AssemblyTitle("")]
[assembly: AssemblyDescription(
"")]
[assembly: AssemblyConfiguration(
"")]
[assembly: AssemblyCompany(
"")]
[assembly: AssemblyProduct(
"")]
[assembly: AssemblyCopyright(
"")]
[assembly: AssemblyTrademark(
"")]
[assembly: AssemblyCulture(
"")]        

//
// 程序集的版本信息包含下列 4 个值:
//
//      主版本
//      次版本
//      内部版本号
//      修订号
//
// 您可以指定所有值,或使用“修订号”和“内部版本号”的默认值,方法为按如下方式 
// 使用“*”:

[assembly: AssemblyVersion(
"1.0.*")]

//
// 要对程序集进行签名,必须指定要使用的密钥。有关程序集签名的更多信息,请参考 
// Microsoft .NET 框架文档。
//
// 使用下面的属性控制用于签名的密钥。
//
// 注意:
//   (*) 如果未指定密钥,则程序集不会被签名。
//   (*) KeyName 是指已经安装在计算机上的
//      加密服务提供程序 (CSP) 中的密钥。KeyFile 是指包含
//       密钥的文件。
//   (*) 如果 KeyFile 和 KeyName 值都已指定,则 
//       发生下列处理:
//       (1) 如果在 CSP 中可以找到 KeyName,则使用该密钥。
//       (2) 如果 KeyName 不存在而 KeyFile 存在,则 
//           KeyFile 中的密钥安装到 CSP 中并且使用该密钥。
//   (*) 要创建 KeyFile,可以使用 sn.exe(强名称)实用工具。
//       在指定 KeyFile 时,KeyFile 的位置应该相对于
//       项目输出目录,即
//       %Project Directory%\obj\<configuration>。例如,如果 KeyFile 位于
//       该项目目录,应将 AssemblyKeyFile 
//       属性指定为 [assembly: AssemblyKeyFile("..\\..\\mykey.snk")]
//   (*) “延迟签名”是一个高级选项 - 有关它的更多信息,请参阅 Microsoft .NET 框架
//       文档。
//
[assembly: AssemblyDelaySign(false)]
[assembly: AssemblyKeyName(
"")]
[assembly: AssemblyKeyFileAttribute(
"..\\..\\snEnterprise.snk")] 
using System;
using System.EnterpriseServices;
using System.Data.SqlClient;

namespace prjEnterprise
{
    
/// <summary>
    
/// Class1 的摘要说明。
    
/// </summary>
    [Transaction(TransactionOption.Required)]public class clsES:ServicedComponent
    {
        
public SqlConnection Conn;

        
public void dbAccess(int pID1, long nScore)
        {
            
try
            {
                SqlConnection Conn 
= new SqlConnection("user id=sa;password=111;Initial Catalog=myDataBase;Data Source=(local);");
                Conn.Open();

                SqlCommand sqlCommand 
= new SqlCommand("UPDATE tbStudentInfo SET Score = " + nScore + " WHERE StudentID = " + pID1, Conn);
                sqlCommand.ExecuteNonQuery();

                ContextUtil.SetComplete();
                Conn.Close();
            }
            
catch(Exception e)
            {
                ContextUtil.SetAbort();

                
throw e;
            }
            
finally
            {

            }
        }
    }

}

注意事项
• 确保使用COM+ 服务的所有项目都有一个强名称。
• 使用COM+ 服务的所有类都必须继承服务组件。
服务组件位于System.EnterpriseServices 命名
空间中。
• 进行调试时,事务在提交或终止前可能会超时。
要避免出现超时,请在事务属性中使用一个超时
属性。在下面的示例中,在完成任何事务时,关
联的方法在超时前都有1,200 秒的执行时间。
[Transaction(TransactionOption.Required,timeo
ut:=1200)]

什么是异常处理
• 异常是正在执行的程序所遇到的任何错误情形或
者意外行为。
• 很多原因都可以引起异常,例如,代码中错误、
操作系统资源不可用、公共语言运行时
(common language runtime)中的意外情况等
等。
• 然而应用程序能够从上述的一些情况中恢复执
行,但是大多数运行时异常是不可恢复的。在这
种情况下,需要一种有效的方法来处理这些异常
并给调用者提供相同的异常。
用结构化的异常处理方法
来处理异常
• 在.NET Web服务中,对异常处理支持的关
键点是由try...catch..finally语句提供的。
• 关键字try放在可能抛出异常的普通处理代
码块之前。
• 关键字catch放在异常处理代码块之前。
• 关键字finally放在那些经常在异常处理后还
需要执行的代码块之前。
• 一旦异常从try代码块中抛出,程序流切换
到后面的第一个catch代码块。

异常类
• Exception 所有异常对象的基类
• SystemException 运行时产生的所有错误的基类
• IndexOutOfRangeException 当一个数组的下标超出范围时
运行时引发
• NullReferenceException 当一个空对象被引用时运行时引发
• InvalidOperationException 当对方法的调用对对象的当前状
态无效时,由某些方法引发
• ArgumentException 所有参数异常的基类
• ArgumentNullException 在参数为空(不允许)的情况下,
由方法引发
• ArgumentOutOfRangeException 当参数不在一个给定范围
之内时,由方法引发
• InteropException 目标在或发生在CLR外面环境中的异常的
基类
• ComException 包含COM 类的HRESULT信息的异常
• SEHException 封装win32 结构异常处理信息的异常
优化异常
• 理解异常是一定会发生的
– 大多数的软件系统都不是百分之百可靠的!
– 要站在异常一定可能会发生的角度来编写异常
处理程序,应对程序有可能发生的错误。
– 建立一个良好的异常处理策略
• 处理未预料的异常
– 确保所有程序的入口都使用了try-catch
– 在catch中截获所有的异常
异常处理注意事项
• 当引发异常时,要提供有意义的文本。
• 要引发异常仅当条件是真正异常;也就是当一个
正常的返回值不满足时。
• 如果你的方法或属性被传递一个坏参数,要引发
一个ArgumentException异常。
• 当调用操作不适合对象的当前状态时,要引发一
个InvalidOperationException异常。
• 要引发最适合的异常。
• 要使用链接异常,它们允许你跟踪异常树。
• 不要为正常或预期的错误使用异常。
• 不要为流程的正常控制使用异常。
• 不要在方法中引发NullReferenceException或
IndexOutOfRangeException异常。
异常处理技术
• 记录异常
– 在文件中记录异常
– 在数据库中记录异常
– 在eventlog中记录异常
• 发送email通知异常
• 异常产生时,用友好(user-friendly)的方式
通知用户
处理错误
• Page_Error事件
• Application_Error事件
• 利用配置文件,自定义错误页面
– <customErrors defaultRedirect="url" mo
de="RemoteOnly">
<error statusCode="code" redirect="url
"></error>
</customErrors>

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Diagnostics;
namespace CheckedExample
{
    
/// <summary>
    
/// WebForm1 的摘要说明。
    
/// </summary>
    public class WebForm1 : System.Web.UI.Page
    {
        
protected System.Web.UI.WebControls.Button btnCal;
        
protected System.Web.UI.WebControls.Label Label2;
        
protected System.Web.UI.WebControls.Label lbResult;
        
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
        
protected System.Web.UI.WebControls.TextBox tbInput;
        
protected System.Web.UI.WebControls.Button btnTC;
        
protected System.Web.UI.WebControls.Button btnTF;
        
protected System.Web.UI.WebControls.Button btnTG;
        
protected System.Web.UI.WebControls.Button btnTCF;
        
protected System.Web.UI.WebControls.Label Label1;
    
        
private void Page_Load(object sender, System.EventArgs e)
        {
            
// 在此处放置用户代码以初始化页面
        }

        
#region Web 窗体设计器生成的代码
        
override protected void OnInit(EventArgs e)
        {
            
//
            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            
//
            InitializeComponent();
            
base.OnInit(e);
        }
        
        
/// <summary>
        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
        
/// 此方法的内容。
        
/// </summary>
        private void InitializeComponent()
        {    
            
this.btnCal.Click += new System.EventHandler(this.btnCal_Click);
            
this.btnTC.Click += new System.EventHandler(this.btnTC_Click);
            
this.btnTF.Click += new System.EventHandler(this.btnTF_Click);
            
this.btnTG.Click += new System.EventHandler(this.btnTG_Click);
            
this.btnTCF.Click += new System.EventHandler(this.btnTCF_Click);
            
this.Load += new System.EventHandler(this.Page_Load);

        }
        
#endregion

        
private void btnCal_Click(object sender, System.EventArgs e)
        {
            
int nInput = int.Parse(tbInput.Text);
            
long nResult=1;
            
for(int i=1;i<=nInput;i++)
                
//    nResult *= i;
                checked{nResult *= i;}
            lbResult.Text 
= nResult.ToString();
    
        }

        
private void btnTC_Click(object sender, System.EventArgs e)
        {
            
int nInput = int.Parse(tbInput.Text);
            
long nResult=1;
            
try
            {
                
for(int i=1;i<=nInput;i++)
                    
//                    nResult *= i;
                    checked{nResult *= i;}
            }
            
catch (OverflowException )
            {
                Response.Write(
"溢出异常!");
            }

            lbResult.Text 
= nResult.ToString();
        }

        
private void btnTF_Click(object sender, System.EventArgs e)
        {
            
int nInput = int.Parse(tbInput.Text);
            
long nResult=1;
            
bool bAllFine = false;
            
try
            {
                
for(int i=1;i<=nInput;i++)
                    
//                    nResult *= i;
                    checked{nResult *= i;}
                bAllFine 
= true;
            }
            
finally
            {
                
if (!bAllFine)
                    Response.Write(
"溢出异常!");
                
else
                    lbResult.Text 
= nResult.ToString();
            }

            
        }

        
private void btnTG_Click(object sender, System.EventArgs e)
        {
            
int nInput = int.Parse(tbInput.Text);
            
long nResult=1;
            
try
            {
                
for(int i=1;i<=nInput;i++)
                    
//                    nResult *= i;
                    checked{nResult *= i;}
                
goto __leave;
            }
            
finally
            {
                lbResult.Text 
= nResult.ToString();
            }
            __leave:
                Response.Write(
"Leave!");

        }

        
private void btnTCF_Click(object sender, System.EventArgs e)
        {
            
int nInput = int.Parse(tbInput.Text);
            
long nResult=1;
            
try
            {
                
for(int i=1;i<=nInput;i++)
                    
//                    nResult *= i;
                    checked{nResult *= i;}
            }
            
catch (OverflowException oe)
            {
                Response.Write(
"溢出异常!");
            }
            
catch(Exception Ex)
            {
                Response.Write(Ex.Message);
            }
            
finally
            {
                lbResult.Text 
= nResult.ToString();
            }
        }

        
    }
}
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace ASPNETAction._04TryCatch
{
    
public class MyException:Exception
   {
         
public MyException():base() {}
         
public MyException(string message):base(message) 
         {
         }
         
public MyException(string message, Exception inner):base(message,inner) {}
          
    }
    
/// <summary>
    
/// _02MyException 的摘要说明。
    
/// </summary>
    public class _02MyException : System.Web.UI.Page
    {
        
protected System.Web.UI.WebControls.Button btnCal;
        
protected System.Web.UI.WebControls.Label Label2;
        
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
        
protected System.Web.UI.WebControls.TextBox tbInput;
        
protected System.Web.UI.WebControls.Label lbResult;
        
protected System.Web.UI.WebControls.Label Label1;
    
        
private void Page_Load(object sender, System.EventArgs e)
        {
            
// 在此处放置用户代码以初始化页面
        }

        
#region Web Form Designer generated code
        
override protected void OnInit(EventArgs e)
        {
            
//
            
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
            
//
            InitializeComponent();
            
base.OnInit(e);
        }
        
        
/// <summary>
        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
        
/// 此方法的内容。
        
/// </summary>
        private void InitializeComponent()
        {    
            
this.btnCal.Click += new System.EventHandler(this.btnCal_Click);
            
this.Load += new System.EventHandler(this.Page_Load);

        }
        
#endregion

        
private void btnCal_Click(object sender, System.EventArgs e)
        {
            
int nInput = int.Parse(tbInput.Text);
            
long nResult=1;
            
try
            {
                
for(int i=1;i<=nInput;i++)
                    
//                    nResult *= i;
                    checked{nResult *= i;}
            }
            
catch (OverflowException oe)
            {
                Response.Write(oe.Message);
                
throw new MyException("自定义异常类报错!");
            }
            
catch(Exception Ex)
            {
                Response.Write(Ex.Message);
            }
            
finally
            {
                lbResult.Text 
= nResult.ToString();
            }
        }
    }
}
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Diagnostics;
namespace CheckedExample
{
    
/// <summary>
    
/// WebForm1 的摘要说明。
    
/// </summary>
    public class Main : System.Web.UI.Page
    {
        
protected System.Web.UI.WebControls.Button btnCal;
        
protected System.Web.UI.WebControls.Label Label2;
        
protected System.Web.UI.WebControls.Label lbResult;
        
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
        
protected System.Web.UI.WebControls.TextBox tbInput;
        
protected System.Web.UI.WebControls.Label Label1;
    
        
private void Page_Load(object sender, System.EventArgs e)
        {
            
// 在此处放置用户代码以初始化页面
        }

        
#region Web 窗体设计器生成的代码
        
override protected void OnInit(EventArgs e)
        {
            
//
            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            
//
            InitializeComponent();
            
base.OnInit(e);
        }
        
        
/// <summary>
        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
        
/// 此方法的内容。
        
/// </summary>
        private void InitializeComponent()
        {    
            
this.btnCal.Click += new System.EventHandler(this.btnCal_Click);
            
this.Error += new System.EventHandler(this.WebForm1_Error);
            
this.Load += new System.EventHandler(this.Page_Load);

        }
        
#endregion

        
private void btnCal_Click(object sender, System.EventArgs e)
        {
            
try
            {
                
int nInput = int.Parse(tbInput.Text);
                
long nResult=1;
                
for(int i=1;i<=nInput;i++)
                    
checked{nResult *= i;}
                lbResult.Text 
= nResult.ToString();
            }
            
catch(OverflowException)//溢出错误异常处理
            {
                
throw new Exception("输入的数字太大,我受不了了!");
            }
            
catch(FormatException)//输入字符格式错误异常处理
            {
                Response.Write(
"嘿!要输入整数!");
            }
            
catch(Exception ee)
            {
                Response.Write(
"发生错误,信息为:"+ee.Message);
            }
        
        }

        
private void WebForm1_Error(object sender, System.EventArgs e)
        {
            
string strMessage = Server.GetLastError().Message;
            
//Response.Write(strMessage);
            Server.ClearError();
            
//以下把信息写入windows日志
            
//要把aspnet用户添加到管理员组中,以便有写注册表权限
            if(!EventLog.SourceExists("mySource"))
                EventLog.CreateEventSource(
"mySource","myLog");
            EventLog Event 
= new EventLog();
            Event.Source 
= "mySource";
            Event.WriteEntry(strMessage,EventLogEntryType.Warning);
            
//EventLog.Delete("myLog");
            throw new Exception("我处理不了,请最高人民法院处理!");


        }
    }
}
原文地址:https://www.cnblogs.com/tianlangshu/p/2491913.html