error 错误
由jvm虚拟抛出
exception 异常
本身程序可以处理的错误
可以用try catch 解决
解决异常的方式:
try----catch-----finally:
try--catch---finally : 异常处理 一旦发生异常并被try--catch捕获,异常右try--catch处理,不会影响下边代码的指向 try: 对有可能存在异常的语句的使用 catch: 一旦try语句发生错误那就就走catch finally: 语句在任何条件下都会执行 --- 资源释放
// jdbc ----> 数据库连接 ---》jdbc
try {
Class person = Class.forName("com.qcby.servlet.Person1");
// jdbc 操作
// ---》 connection
// ---》 statment 对象
// ---》 sql 语句 ---》出错 100次 ---》100次连接
} catch (ClassNotFoundException e) {
System.out.println("****************");
e.printStackTrace();
}finally {
//资源释放
// --》 关闭statment对象
// ---》 关闭 connection对象
}
System.out.println("/*/*/*/*/*");
}
throws-------抛出异常
throws --->不处理异常,抛出异常,→该异常的使用者解决 :那么继续抛出,要么 tryCatchJiejye throw :是自定义异常
public static void run() throws Exception {
int a = 10;
int b = 100;
if(a < b){
throw new Exception("发生异常");
}
}