异常处理
异常:Exception
Throwable:异常和错误的父类
错误:Error 是由于内存溢出、硬件损坏等原因造成程序无法运行。不治之症,不需要程序猿处理。
异常:Exception 在程序运行过程中,出现的不正常现象。伤风感冒,需要程序猿处理。
Exception是所有异常类型的父类。
异常有两种类型:
1.运行时异常:在编译时期不强制要求处理的异常。运行时异常都有一个父类 RuntimeException
2.编译时异常(受检查异常):在编译时期强制要求对其进行处理。
1>使用try-catch-finally 代码块进行处理。
2>使用throws关键字抛出异常。
运行时异常:
ArrayIndexOutOfBoundsException:数组下标越界异常
ClassCastException:类型转换异常
NullPointerException:空指针异常
InputMismatchException:输入不匹配异常
ArithmeticException:算术异常
异常处理机制是很多计算机语言中都有的处理机制。java是一门面向对象的语言。异常也满足面向对象的思想,java中把异常封装成类,其中包括一些属性:如异常信息、异常行数等等。
try {
运行可能出现异常的代码。
如果在try代码块中的某行代码抛出了异常,该行代码之后的内容都无法继续执行。
} catch(异常类型 对象名) {
将抛出的异常对象进行捕捉并处理。
在try-catch后的代码依然可以继续执行。
}
.....
catch(异常类型 对象名) {
catch代码块可以有多个,可以针对不同的异常进行分别处理。当try中抛出异常对象,catch代码块会从上至下依次匹配类型。如果匹配成功,则进入该catch代码块中进行处理。
如果有多个catch代码块的时候需要注意,如果几个异常类型存在父子关系,必须从子到父。
} finally {
最终地、最后地。无论try中是否抛出异常,无论catch是否捕捉并处理,finally中的内容始终会被执行。通常用于关闭资源。
finally可以有可以没有。
如果在try-catch-finally中有return,先执行finally后执行return
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import java.util.Scanner; 2 public class Demo{ 3 public static void main(String[] args){ 4 //arr[3] = 500; 5 /*Scanner input = new Scanner(System.in); 6 int num = input.nextInt(); 7 */ 8 try{ 9 String s = null; 10 System.out.println(s.equals("")); 11 int[] arr = new int[3]; 12 arr[0] = 12; 13 arr[1] = 22; 14 arr[50] = 55; 15 int x = 90; 16 int y = 0; 17 int z = x/y; 18 System.out.println("z:" + z); 19 System.out.println("这是try的尾巴"); 20 21 }catch(IndexOutOfBoundsException e2){ 22 System.out.println("数组下标越界了啊"); 23 }catch(NullPointerException e3){ 24 System.out.println("空指针异常了啊"); 25 //return; 26 //System.exit(0);//强制关闭JVM 27 }catch(ArithmeticException e){ 28 System.out.println("出现算术异常了啊!"); 29 }catch(RuntimeException e4){ 30 System.out.println("运行时异常"); 31 }catch(Exception e5){ 32 System.out.println("大异常啊"); 33 }finally{ 34 System.out.println("我是finally...."); 35 } 36 System.out.println("这是后面"); 37 } 38 }
------------------------------------------------------------------------
throws:方法声明异常以便抛出。
throw:程序猿手动抛出异常。
------------------------------------------------------------------------
Exception中的方法:
void | printStackTrace();:打印异常详细信息
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /* 2 抽象父类:Employee 3 */ 4 public abstract class Employee{ 5 //属性 6 private String name; 7 private int age; 8 private String sex; 9 private int salary;//底薪 10 //构造方法 11 public Employee(){} 12 public Employee(String name,int age,String sex,int salary){ 13 this.name = name; 14 this.age = age; 15 this.sex = sex; 16 this.salary = salary; 17 } 18 //getset方法 19 public void setName(String name){ 20 this.name = name; 21 } 22 public String getName(){ 23 return name; 24 } 25 public void setAge(int age){ 26 this.age = age; 27 } 28 public int getAge(){ 29 return age; 30 } 31 public void setSex(String sex){ 32 this.sex = sex; 33 } 34 public String getSex(){ 35 return sex; 36 } 37 public void setSalary(int salary){ 38 this.salary = salary; 39 } 40 public int getSalary(){ 41 return salary; 42 } 43 //计算工资的抽象方法 44 public abstract int getMoney(); 45 46 } 47 /* 48 子类:经理类 49 */ 50 class Manager extends Employee{ 51 //属性 52 private int bonus;//奖金 53 //构造方法 54 public Manager(){} 55 public Manager(String name,int age,String sex,int salary,int bonus){ 56 super(name,age,sex,salary); 57 this.bonus = bonus; 58 } 59 public void setBonus(int bonus){ 60 this.bonus = bonus; 61 } 62 public int getBonus(){ 63 return bonus; 64 } 65 66 //重写父类抽象方法 67 public int getMoney(){ 68 return getSalary() + bonus;//底薪+奖金 69 } 70 } 71 /* 72 子类:销售 73 */ 74 class Saler extends Employee{ 75 //属性 76 private int num;//个数 77 //构造方法 78 public Saler(){} 79 public Saler(String name,int age,String sex,int salary,int num){ 80 super(name,age,sex,salary); 81 this.num = num; 82 } 83 public void setNum(int num){ 84 this.num = num; 85 } 86 public int getNum(){ 87 return num; 88 } 89 //重写父类抽象方法 90 public int getMoney(){ 91 return getSalary() + 100*num;//底薪 + 100*个数; 92 } 93 } 94 /* 95 子类:普通员工 96 */ 97 class Worker extends Employee{ 98 //构造方法 99 public Worker(){} 100 public Worker(String name,int age,String sex,int salary){ 101 super(name,age,sex,salary); 102 } 103 //重写父类抽象方法 104 public int getMoney(){ 105 return getSalary(); 106 } 107 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /* 2 抽象父类:Employee 3 */ 4 public abstract class Employee{ 5 //属性 6 private String name; 7 private int age; 8 private String sex; 9 private int salary;//底薪 10 //构造方法 11 public Employee(){} 12 public Employee(String name,int age,String sex,int salary){ 13 this.name = name; 14 this.age = age; 15 this.sex = sex; 16 this.salary = salary; 17 } 18 //getset方法 19 public void setName(String name){ 20 this.name = name; 21 } 22 public String getName(){ 23 return name; 24 } 25 public void setAge(int age){ 26 this.age = age; 27 } 28 public int getAge(){ 29 return age; 30 } 31 public void setSex(String sex){ 32 this.sex = sex; 33 } 34 public String getSex(){ 35 return sex; 36 } 37 public void setSalary(int salary){ 38 this.salary = salary; 39 } 40 public int getSalary(){ 41 return salary; 42 } 43 //计算工资的抽象方法 44 public abstract int getMoney(); 45 46 } 47 /* 48 子类:经理类 49 */ 50 class Manager extends Employee{ 51 //属性 52 private int bonus;//奖金 53 //构造方法 54 public Manager(){} 55 public Manager(String name,int age,String sex,int salary,int bonus){ 56 super(name,age,sex,salary); 57 this.bonus = bonus; 58 } 59 public void setBonus(int bonus){ 60 this.bonus = bonus; 61 } 62 public int getBonus(){ 63 return bonus; 64 } 65 66 //重写父类抽象方法 67 public int getMoney(){ 68 return getSalary() + bonus;//底薪+奖金 69 } 70 } 71 /* 72 子类:销售 73 */ 74 class Saler extends Employee{ 75 //属性 76 private int num;//个数 77 //构造方法 78 public Saler(){} 79 public Saler(String name,int age,String sex,int salary,int num){ 80 super(name,age,sex,salary); 81 this.num = num; 82 } 83 public void setNum(int num){ 84 this.num = num; 85 } 86 public int getNum(){ 87 return num; 88 } 89 //重写父类抽象方法 90 public int getMoney(){ 91 return getSalary() + 100*num;//底薪 + 100*个数; 92 } 93 } 94 /* 95 子类:普通员工 96 */ 97 class Worker extends Employee{ 98 //构造方法 99 public Worker(){} 100 public Worker(String name,int age,String sex,int salary){ 101 super(name,age,sex,salary); 102 } 103 //重写父类抽象方法 104 public int getMoney(){ 105 return getSalary(); 106 } 107 }