看这个代码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函数中重新被捕获才对,但是源程序中我做注释的地方却并没有被执行,这是为何?
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函数中重新被捕获才对,但是源程序中我做注释的地方却并没有被执行,这是为何?
这样试试 先确定有没抛异常
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
的意思差不多的,顶多只会进入一个条件,实际情况也是这样的,顶多只会引发一个类型的异常,不会同时引发这两个异常。
{}
catch(Exception e)。
{}试试
{}
catch(Exception 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;
} }
改为: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;
}就行了.
代码都改好给你了,我想原因不用我多解释,你就知道了.