看这个代码import java.sql.*;
class DB
{
public static void main(String arg[])
{
Connection c=null;
try{
c=DB.getConnection("sa","12");
}catch(ClassNotFoundException e)
{
System.out.println(e);
}catch(SQLException e)
{
System.out.println(e);//这个地方,并没有被执行
}
System.out.println(c==null);
                if(c!=null) 
try{
c.close();
}catch(SQLException e)
{
System.out.println(e);
}
}
public static Connection getConnection(String userName,String ps) throws ClassNotFoundException,SQLException
{
Connection c=null;
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
c=DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=Northwind",userName,ps);
}catch(ClassNotFoundException e)
{
System.out.println("1");
throw e;
}catch(SQLException e)
{
System.out.println("2");
throw e;
}finally
{
return c;
}
}
}运行结果为:
2
true
数据库用户名为sa,密码为123,我在这里填的密码是12,因此在调用函数DB.getConnection("sa","12")时会产生SQLException,并且这么异常已经在此函数中被捕获了,但是在catch 块中,这个SQLException又被重新抛出,按理说应该在main函数中重新被捕获才对,但是源程序中我做注释的地方却并没有被执行,这是为何?

解决方案 »

  1.   

    catch(Exception e)
    这样试试 先确定有没抛异常
      

  2.   

    顺序执行的
    try{
                c=DB.getConnection("sa","12");
            }catch(ClassNotFoundException e)//////先执行这个,如果引发的是这个异常就进入
            {
                System.out.println(e);)//////如果符合就执行这个        }catch(SQLException e)//////如果引发的不是上面的异常,就再跟这个匹配
            {
                System.out.println(e);//这个地方,并没有被执行
            }
    catch_catch

    if_else if
    的意思差不多的,顶多只会进入一个条件,实际情况也是这样的,顶多只会引发一个类型的异常,不会同时引发这两个异常。
      

  3.   

    按照LZ的意思还是先用try
    {}
    catch(Exception e)。
    {}试试
      

  4.   

    按照LZ的意思还是先用try
    {}
    catch(Exception e)
    {}试试
      

  5.   

    问:"但是在catch 块中,这个SQLException又被重新抛出,按理说应该在main函数中重新被捕获才对,但是源程序中我做注释的地方却并没有被执行,这是为何?"答:原因是:你的代码写错了
     将:
    public static Connection getConnection(String userName,String ps) throws ClassNotFoundException,SQLException
        {
            Connection c=null;
            try{
                Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
                c=DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=Northwind",userName,ps);
            }catch(ClassNotFoundException e)
            {
                System.out.println("1");
                throw e;
            }catch(SQLException e)
            {
                System.out.println("2");
                throw e;
            }finally
            {
                return c;
            }
        }
    改为:public static Connection getConnection(String userName,String ps) throws ClassNotFoundException,SQLException
        {
            Connection c=null;
            try{
                Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
                c=DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=Northwind",userName,ps);
            }catch(ClassNotFoundException e)
            {
                System.out.println("1");
                throw e;
            }catch(SQLException e)
            {
                System.out.println("2");
                throw e;
            }finally
            {
              //  return c;        }  return c;
        }就行了.
    代码都改好给你了,我想原因不用我多解释,你就知道了.
      

  6.   

    一句话,将return c;从finally{}中移出来,程序就达到你的目的了.
      

  7.   

    七楼说的对,但是一旦产生异常,那么最后的那个return c是执行不到的,这也是我最初为什么把这个return放入finally的原因.但我还是不知道为什么,按我的写法为什么行不通.
      

  8.   

    答:"那么最后的那个return c是执行不到的",不是这样的.无论发生什么事,finally{}一定都要执行的.