异常
异常概念
异常:在Java语言中,将程序执行中发生的不正常情况称为“异常”,异常就是程序运行过程中出现了不正常现象导致程序的中断。
异常结构
在 Java 中,所有的异常都有一个共同的祖先 Throwable(可抛出),然后又可分为两类:
Error:(错误)是程序无法处理的错误,表示运行应用程序中较严重问题,Java虚拟机无法解决的问题,与代码编写者操作无关
Exception:(异常)它是因为编译错误或者是外在因素导致的问题,可以使用针对性的代码进行处理,他有一个重要的子类RuntimeException,RuntimeException 类及其子类表示“JVM 常用操作”引发的错误,而Exception又可以分为两大类运行时异常和非运行时异常(编译异常)
1)运行时异常:(RuntimeException)这种系统异常可以处理也可以不处理,所以编译器不强制用try…catch处理或用throws声明,所以系统异常也称为unchecked异常
2)编译期异常:(Checked Exception)除去运行期的异常都是编译期异常,也称为检测异常
下边是一个图解:
public static void main(String[] args) {
/*
ArrayIndexOutOfBoundsException数组下标越界
*/
int[] arr = new int[10];
arr[10] = 5;
System.out.println(arr[10]);
/*
ArithmeticException算数异常
*/
int a = 2;
System.out.println(a/0);
/*
ClassCastException转型异常
*/
Demo01 demo01 = (Demo01) new Object();
/*
NullPointerException空指针异常
*/
String s = null;
s.length();
}
异常处理机制
Java语言使用异常处理机制为程序提供了错误处理的能力,通常通过几个关键字来处理try,catch,finally,throws,throw
1.try catch(捕获)
try:它的作用是用来检测不安全的代码块,如果try代码块中出现了异常,下面的代码块将不会执行,就会跳到异常处理代码块中也就是catch代码块中.
catch:把抓到的类型匹配的异常捕获,保证程序能继续运行下去
跟在try后,catch可以有一个或多个,catch中是需要捕获的异常,当try中的代码出现异常时,出现异常下面的代码不会执行,马上会跳转到相应的catch语句块中,如果没有异常不会跳转到catch中
public static void main(String[] args) {
String s = null;
int a = 10;
int b = 0;
try { //在这里是一个try对应一个catch,同是一个try可以对应多个catch
System.out.println(s.length());
}catch (NullPointerException n){
System.out.println("空指针异常");
}
try {
System.out.println(a/b);
}catch (ArithmeticException arithmeticException){
System.out.println("算数异常");
}
}
2.finally
finally该内容总是会执行的,只能有一个finally语句,不管是出现异常,还是没有出现异常,finally里的代码都执行
finally可以和catch分开使用,但是不能和try分开使用,必须和try一起使用
public static void main(String[] args) {
int[] arr = new int[5];
try { //tyr catch finally
arr[5] = 10;
}catch (Exception exception){
System.out.println("出现异常");
}finally {
System.out.println("输出");
}
try { // try finally
arr[5] = 10;
}finally {
System.out.println("输出");
}
}
throws(抛出)
在定义方法时,可以使用throws声明,把异常抛出就是为了提醒方法的使用者,有异常需要预处理.在处理异常时,是选择捕获处理还是抛出处理
public class Demo05 {
public static void main(String[] args) {//处理异常
try {
Demo05.test1();
} catch (UnsupportedEncodingException e) {
System.out.println("输入格式有误");
}
}
public static void test1() throws UnsupportedEncodingException {//抛出异常
Demo05.test2();
}
public static void test2() throws UnsupportedEncodingException {//抛出异常
String s = "asd";
byte[] bytes = s.getBytes("UTF-80");
System.out.println("zxc");
}
}
任何方法都可以使用throws关键字声明异常类型,包括抽象方法。
子类重写父类中的方法,子类方法不能声明抛出比父类类型更大的异常
使用了throws的方法,调用时必须处理声明的异常,要么使用try-catch,要
么继续使用throws声明。
public class Demo06 {//测试类
public static void main(String[] args) {
Son son = new Son();
son.test1();
}
}
public abstract class Person {//抽象类,父类抛出异常
public abstract void test1()throws ArrayIndexOutOfBoundsException;
public abstract void test2()throws UnsupportedEncodingException;
public abstract void test3()throws IOException;
public abstract void test4()throws Exception;
}
public class Son extends Person{
@Override
public int hashCode() {
return super.hashCode();
}
@Override
public void test1() throws ArrayIndexOutOfBoundsException {//捕获异常
try {
int[] arr = new int[3];
arr [3] = 5;
}catch (ArrayIndexOutOfBoundsException arrayIndexOutOfBoundsException){
System.out.println("数组下标越界");
}
}
@Override
public void test2() throws UnsupportedEncodingException {
}
@Override
public void test3() throws IOException {
}
@Override
public void test4() throws Exception {
}
}
throw
throw关键字是用来显示抛出异常,抛出的时候抛出的是一个异常类的实例化对象
在异常处理中,try捕获的是一个异常对象,有些异常也可以自己抛出
public class Demo07 {
public static void main(String[] args) {
try {
Demo07.print(101);
} catch (UnsupportedEncodingException e) {
//e.printStackTrace();
System.out.println(e.getMessage());
}
}
public static char print(int score) throws UnsupportedEncodingException {
if (score<0 || score>100){
throw new UnsupportedEncodingException("输入有误");//此时我们抛出一个异常
}else if(score >=90){
return 'A';
}else{
return 'B';
}
}
}
throw表示的是抛出一个具体的异常类型,用在方法体内,跟的是异常对象名
只能抛出一个异常类型名字
抛出了异常,执行throw则一定抛出了某种异常,可以使用try来捕获或者是使用throws来声明
自定义异常
简单来说就是自己定义的一个异常,避免与标准异常混淆
首先我们需要自定义一个类让他去继承Exception或者是RuntimeException
然后需要提供两个构造方法,一个有参一个无参
例如我们在这里自定义一个异常:
public class DiyException extends Exception{
public DiyException() {
}
public DiyException(String message) {
super(message);
}
public static void delta(int a, int b, int c) throws DiyException {
//这里是一个构成三角形的条件,不满足就会抛出一个自定义异常
if (a+b<c || a+c<b || b+c<a || Math.abs(a-b)>c || Math.abs(a-c)>b || Math.abs(b-c)>a){
throw new DiyException("输入有误");
}else{
System.out.println("可以构成三角形");
}
}
}
public class Test {
public static void main(String[] args) {
try {
DiyException.delta(1, 4, 5);
} catch (DiyException e) {
System.out.println(e.getMessage());
}
}
}