Java抛异常处理机制

 void addbookadmin() throws ClassNotFoundException, SQLException {
           String sql="insert into admin (bookid, wrid, zbid, soldbookid, booknumberid) values (?,?,?,?,?)";
            Class.forName(driver);
            try {
                Connection conn = DriverManager.getConnection(url, user, sqlpassword);
                PreparedStatement ps1 = conn.prepareStatement(sql);
                ps1.setString(1, this.bookid);
                ps1.setString(2, this.wrid);
                ps1.setString(3, this.zbid);
                ps1.setString(4, this.soldbookid);
                ps1.setString(5, this.booknumberid);

                ps1.executeUpdate();
                //ps.executeQuery();
                ps1.close();    
                conn.close();
                
            }    
            catch(SQLException ex) {
                System.out.println("添加书籍失败!");
            }
            
        }

为什么一执行到 ps1.executeUpdate();就抛出异常,后面就不执行了 

异常信息打印看下。

catch(SQLException ex) {
                System.out.println("添加书籍失败!");
            }

改成

catch(SQLException ex) {
            ex.printStackTrace();
                System.out.println("添加书籍失败!");
            }

try{}catch(){}finally{}
try中写可能发生异常的代码块,发生异常的语句后的代码不执行;
catch中写异常处理,例如记录异常日志,catch的异常需要是抛出异常或抛出异常的基类;
必须执行的代码块例如文件流关闭写在finally中;