我是这样写的
        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,还是一样,为什么捕获不到阿!

解决方案 »

  1.   

    为什么try catch不能捕获错误,但finally倒是执行了,
    捕获不到错误 finally 也要执行   
    有没有错误都执行
      

  2.   

    如果 是cnn.Open()的错误 数据据根本没有打开  
                      Catch e As Exception
              这句 也就更不对了  conn.Execute("insert into test values ('has error!')")
      

  3.   

    不是,一个是conn一个是cnn,不一样的
    conn我保证连通的,这是调试用的
      

  4.   

    不然,我都没办法知道程序运行的流程,conn得连接我没贴上来,但肯定是可以用的,不然finally也不会打出来
    问题就在于cnn.open,产生了错误,但没有去try
      

  5.   

    没测试过,但我想如果异常是由于数据库的操作产生的,那么
           Catch e As Exception
                conn.Execute("insert into test values ('has error!')")
            Finally
                conn.Execute("insert into test values ('finally')")
            End Try
    这两条都不会执行,你能在数据库中找到finally,那是因为是正常情况下插入的。
      

  6.   

    请楼上二位注意,conn和cnn是两个东西!
    conn是调试用的,错误是发生在try里的cnn.open
      

  7.   

    捕获不到错误 finally 也要执行   
    有没有错误都执行
      

  8.   

    加个断点 看看、   conn.Execute("insert into test values ('has error!')")执行了没有!!
    不过你这么做很不合逻辑
    要是     conn.Execute("insert into test values ('has error!')")这句错误了 
    你的程序就崩溃了
      

  9.   

    我知道finally必定要执行,你们要注意环境,这段代码是在Window Service里运行的,不能直接运行的,要注册服务的,也不能有msgbox弹出的
    我很清楚try catch finally得关系,但是现在是在Window Service里。
      

  10.   

    我们站在conn.Execute("insert into test values ('has error!')")这句话不会错的基础上来讨论,这只是调试,不是实际用的。
    总结一下:
    try里面发生了错误,catch得语句没有运行,就是这样,不要把重点放在finally上。并且是在window service里,如果是一般的application,我想我也不会来问这种问题了。
      

  11.   

    你清楚关系 这句什么意思:为什么try catch不能捕获错误,但finally倒是执行了,
      

  12.   

    我看有可能是异常的调用堆栈问题!!你在调用这个过程的地方是不是也有 try catch 
    出错处理可能转移到哪里了
      

  13.   

    window service里  你也可以调试呀!!设计的时候模拟环境呀
      

  14.   

    查一下cnn.State然后throw Exception行不行?
      

  15.   

    "为什么try catch不能捕获错误,但finally倒是执行了"
    我只是说明一下,不要一直盯着这句话好哇