我是这样写的
Try
If mapping.DBStyle = "mdb" Then
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=" + mapping.Password + ";User ID=" + mapping.UserId + ";Data Source=" + mapping.DataSource
cnn.ConnectionString = connString
cnn.Open()
End If ...... Catch e As Exception
conn.Execute("insert into test values ('has error!')")
Finally
conn.Execute("insert into test values ('finally')")
End Try其中conn.Execute("insert into test values ('has error!')")是往数据库里写调试信息的
数据库里我只能看到"finally",而"has error!"却没有,但肯定是有连接不上的(人为的使其错误)
后来我把Excepton改成System.Runtime.InteropServices.COMException,还是一样,为什么捕获不到阿!
Try
If mapping.DBStyle = "mdb" Then
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=" + mapping.Password + ";User ID=" + mapping.UserId + ";Data Source=" + mapping.DataSource
cnn.ConnectionString = connString
cnn.Open()
End If ...... Catch e As Exception
conn.Execute("insert into test values ('has error!')")
Finally
conn.Execute("insert into test values ('finally')")
End Try其中conn.Execute("insert into test values ('has error!')")是往数据库里写调试信息的
数据库里我只能看到"finally",而"has error!"却没有,但肯定是有连接不上的(人为的使其错误)
后来我把Excepton改成System.Runtime.InteropServices.COMException,还是一样,为什么捕获不到阿!
捕获不到错误 finally 也要执行
有没有错误都执行
Catch e As Exception
这句 也就更不对了 conn.Execute("insert into test values ('has error!')")
conn我保证连通的,这是调试用的
问题就在于cnn.open,产生了错误,但没有去try
Catch e As Exception
conn.Execute("insert into test values ('has error!')")
Finally
conn.Execute("insert into test values ('finally')")
End Try
这两条都不会执行,你能在数据库中找到finally,那是因为是正常情况下插入的。
conn是调试用的,错误是发生在try里的cnn.open
有没有错误都执行
不过你这么做很不合逻辑
要是 conn.Execute("insert into test values ('has error!')")这句错误了
你的程序就崩溃了
我很清楚try catch finally得关系,但是现在是在Window Service里。
总结一下:
try里面发生了错误,catch得语句没有运行,就是这样,不要把重点放在finally上。并且是在window service里,如果是一般的application,我想我也不会来问这种问题了。
出错处理可能转移到哪里了
我只是说明一下,不要一直盯着这句话好哇