java 异常处理机制
Throwable 类 异常的顶级类
子类
Error :java运行系统级别的错误
栈内存溢出
Exception:程序级别的错误
网络故障
文件损坏
设备错误
用户输入非法
可以通过捕获机制类解决
try 语句
try{
可能出现异常的代码片段
}
try{}这个是发现问题的语句
catch 语句
catch(Exception_Type e){
解决问题的代码片段
}
catch语句是用来捕获try语句中出现 并针对该异常解决,catch语句块可以出现多次
捕获异常:应该在最后一个catch 中加入Exception 可以保证程序不会因为没有捕获到一个未知的异常而中断, 而且这个Exception 应该放在所有catch 的最后一个..
package day31;
public class Demo01 {
public static void main(String[] args){
try{
String str = null;
System.out.println(str.length());
}catch(NullPointerException e){
System.out.println("Null Pointer Exception");
}
System.out.println("Over");
try{
String str1 = "";
System.out.println(str1.length());
System.out.println(str1.charAt(3));
}catch(NullPointerException e){
System.out.println("Null Pointer Exception");
}catch(StringIndexOutOfBoundsException e){
System.out.println("String Index Out Of Bounds Exception");
}
System.out.println("Over");
try{
String str2 = "abc";
System.out.println(str2.length());
System.out.println(str2.charAt(1));
System.out.println(Integer.parseInt(str2));
}catch(NullPointerException e){
System.out.println("Null Pointer Exception");
}catch(StringIndexOutOfBoundsException e){
System.out.println("String Index Out Of Bounds Exception");
}catch(Exception e){
System.out.println("Unknown Error");
}
System.out.println("Over");
}
}
throw 语句
throw e;
throw 用来主动的抛出某一个异常的实例
我们定义的方法在运行过程中出现了错误 而这个错误如何解决应该由调用者决定时
遇到一个不符合逻辑的操作时我们可以把它当作一个异常去处理
package day31;
public class Person {
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
if (age > 1000) {
throw new RuntimeException("不合法的年龄");
}
this.age = age;
}
}
package day31;
public class Demo02 {
public static void main(String[] args){
Person p = new Person();
p.setAge(100);
}
}
package day31;
public class Demo03 {
public static void main(String[] args) {
Person p1 = new Person();
try {
p1.setAge(10001);
} catch (Exception e) {
System.out.println(e);
}
}
}
thorws 声明
在当前方法中出现异常,但是处理这个异常的责任不是当前方法管,这时候就把异常抛出去
谁调用 谁来解决
当程序遇到满足语法要求,但是不符合业务逻辑的 也可以使用thorws 一个异常
通常情况下方法中 throw一个异常实例我必须处理这个异常 处理的方式有两种
1,为throw 添加try{}catch(){}
2,在当前方法是声明该类异常的抛出,通知调用者处理该异常
我们定义的方法中可能会出现错误,无论是否为我们主动抛出的,但只要是方法中出现的异常不在方法中处理的,我们通常在声明方法是同事申明可能会抛出的异常,通知调用者必须捕获
package day31;
/**
*
* 定义方法时可以声明要抛出的异常
*使用throws 语句
*/
public class Demo04 {
/**
*当方法中调用的其他方法声明了throws时,
*在当前方法中try-catch解决这个问题
*在当前方法上声明throws 继续将异常向外抛出(在main上就不要抛出了 jvm 会直接kill)
*/
public static void main(String[] args){
//连接数据库
try {
connectionDB("192.168.1.2");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("保存数据");
System.out.println("OK");
}
/**
*连接数据库
*throws 可以抛出多个异常,每一个异常用 , 隔开
*不要定义方法中部会抛出的异常
*
*方法中throw 什么异常,throws 就要定义什么异常
*
*/
public static void connectionDB(String url) throws Exception{
if("192.168.1.1".equals(url)){
System.out.println("数据库连接成功");
}else{
//
throw new Exception("连接数据库异常");
}
}
}
package day31;
/**
*
*方法中抛出RuntimeException 及其子类时
*不需要再该方法声明定义throws
*RuntimeException 称为非检查异常
* 就是当编译的过程中发现了这类异常的抛出时,是一个编译通过的
*但是除了这个其他的抛出的异常类型若不写throws 或者捕获 编译时不通过的
*常见的RunntimeException 的子类
*NullPointerException
*ArrayIndexOutOfBoundsException
*NumberFormatException
*ClassCastException
*/
public class Demo05 {
public static void main(String[] args){
}
public static void connectionDB(){
throw new RuntimeException("RunError");
}
}
finally块
finally{
代码片段
}
finally出现在try语句的最后
finally块中的语句是必然执行的无论try中是否出现异常
用于收尾工作
package day31;
public class Demo06 {
public static void main(String[] args){
try{
String str = null;
System.out.println(str.length());
}catch(Exception e){
System.out.println("Error");
}finally{//无论try中的语句是否报错,finally居于块都会执行
System.out.println("FINALLY");
}
System.out.println("Over");
}
}
package day31;
public class Demo07 {
public static void main(String[] args){
try{
String age = "abc";
System.out.println("ConnectMysql");
System.out.println("SaveAge:"+Integer.parseInt(age));
}catch(Exception e){
System.out.println("Error");
}finally{//收尾工作 关闭数据库连接
System.out.println("CloseConnection");
}
System.out.println("ExitSystem.");
System.out.println();
try{
String age1 = "123";
System.out.println("ConnectMysql");
System.out.println("SaveAge:"+Integer.parseInt(age1));
}catch(Exception e){
System.out.println("Error");
}finally{//收尾工作 关闭数据库连接
System.out.println("CloseConnection");
}
System.out.println("ExitSystem.");
}
}
package day31;
public class Demo08 {
public static void main(String[] args){
System.out.println(test(null)+","+test("0")+","+test(""));
// 1 0 2
//但是 finally 必须执行 so 4,4,4
}
public static int test(String str){
try{
return str.charAt(0) - '0';
}catch(NullPointerException e){
return 1;
}catch(RuntimeException e){
return 2;
}catch(Exception e){
return 3;
}finally{//必须执行
return 4;//so 不要再finally 里面添加 return 不然方法只能返回finally中的return
}
}
}
重写方法时的异常处理
父类方法中通过throws 声明了某些异常的抛出
子类重写时可以不声明throws
子类重写时可以抛出父类抛出的异常的子类异常
父类抛出RuntimeException
子类可以抛出NullPointerException
子类重写时可以只抛出父类抛出的部分异常
父类throws 3个
子类可以throws 2个
子类重写时不能抛出父类方法中没有抛出的额外异常
父类throws 3个
子类不能throws 4个
子类重写是不能抛出父类方法中抛出的异常的父类异常
父类throws RuntimeException
子类不能throws Exception
package day31;
public class Demo08 {
public static void main(String[] args){
System.out.println(test(null)+","+test("0")+","+test(""));
// 1 0 2
//但是 finally 必须执行 so 4,4,4
}
public static int test(String str){
try{
return str.charAt(0) - '0';
}catch(NullPointerException e){
return 1;
}catch(RuntimeException e){
return 2;
}catch(Exception e){
return 3;
}finally{//必须执行
return 4;//so 不要再finally 里面添加 return 不然方法只能返回finally中的return
}
}
}
try{
}finalyy{
/**
*这种组合通常是:
*不关心是否有异常,但是关心某段代码不管是否出现异常都要执行
*/
}