'-----写日志方法:----------- Public Shared Function errLog(ByVal err As String) As String Try '写入数据库日志表
Catch ex As Exception '写入windows event log End Try End Function '-----调用:------------ Try Catch ex As Exception Utility.errLog("Webpage error log in " + Request.RawUrl() + "(user:" + Session("loginid") _ + "; IP:" + Request.ServerVariables("REMOTE_ADDR") + ") - " + ex.ToString()) End Try
class Program { public static void Main(string[] args) { try { } catch (OtherException1 e) { throw new OtherException1(); } } } class OtherException1 : Exception { public OtherException1() { //写日志1 throw new Exception("OtherException1"); } } class OtherException2 : Exception { public OtherException2() { //写日志2 throw new Exception("OtherException2"); } }
{
//可能发生异常的语句
}
catch(Exception)
{
//捕获异常,catch语句可以多级嵌套,以实现精准的异常捕获
}
finally
{
//一定要执行的语句放在这里,例如关闭文件等
}
try
{
//可能发生异常的语句
}
catch(Exception)
{
//捕获异常,写如日志
write.log();
}
finally
{
//一定要执行的语句放在这里,例如关闭文件等
}
Public Shared Function errLog(ByVal err As String) As String
Try
'写入数据库日志表
Catch ex As Exception
'写入windows event log
End Try End Function
'-----调用:------------
Try
Catch ex As Exception
Utility.errLog("Webpage error log in " + Request.RawUrl() + "(user:" + Session("loginid") _
+ "; IP:" + Request.ServerVariables("REMOTE_ADDR") + ") - " + ex.ToString())
End Try
void button1_Click(object sender,EventArgs e){
TryNoDebug(Do1);
}class Common{
static void TryNoDebug(MethodInvoker method){
if(Debugger.IsAttached){
method();
}else{
try...
}
}
}
{
public static void Main(string[] args)
{
try
{ }
catch (OtherException1 e)
{
throw new OtherException1();
}
}
} class OtherException1 : Exception
{
public OtherException1()
{
//写日志1
throw new Exception("OtherException1");
}
} class OtherException2 : Exception
{
public OtherException2()
{
//写日志2
throw new Exception("OtherException2");
}
}