1、定义:
1、异常(Exception)就是在程序的运行过程中出现的不正常现象导致了程序中断。
2、异常(Exception) ≠ 错误(error)
3、在java中, 把一些不正常现象进行了抽象, 形成了一些异常类。
2、常见异常:
1.空指针异常: NullPointerException
2.类型转换异常:ClassCastException
3.算术异常: ArithmeticException
3、异常处理机制:
快捷键:Ctrl+Alt+t
try catch finally
try {
可以发生异常的代码块
} catch(Exception e) {
1、捕获异常。
2、将异常封装到Exception对象e,传递给catch。
3、得到异常后,程序员自己处理。
4、如果没有发生异常,catch不执行。
} finally {
1、不管是否发生异常,finally一定会执行。
2、因此通常将释放资源的代码写在这里。
}
public class Exception01 {
public static void main(String[] args) {
try {
String name = "hsf";
int num = Integer.parseInt(name);
System.out.println("异常后面的代码"); // 1、异常后面的代码不执行
} catch (NumberFormatException e) {
System.out.println(e.getMessage()); ///2、直接跳入执行catch里面代码
} finally {
System.out.println("我是finally里面的代码"); ///3、finally代码执行
}
System.out.println("helloWord"); // 4、输出后面代码
}
}
可以多个catch同时使用:但是要把Exception的子类放在前面。
package com.hsf.exception;/*
* @author HSF
* @version 8.0
*/
import javax.sound.midi.Soundbank;
import java.util.Scanner;
public class Exception01 {
public static void main(String[] args) {
people people = new people();
people = null;
int num1 = 10;
int num2 = 0;
try {
System.out.println(people.name);
int result = num1 / num2;
}catch (NullPointerException e) {
System.out.println(e.getMessage());
} catch (ArithmeticException e) {
System.out.println(e.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
}
}
class people {
public String name = "jack";
}
可以进行try-finally搭配使用,这种写法相当于没有捕获异常,因此程序如果发生了异常会直接退出。应用场景:执行一段代码,不管是否发生异常,都必须执行某个业务逻辑。
throws
运行时异常有默认的throws,可以不用处理;编译时异常必须要处理,没有默认throws
public void f1() throws Exception {
}
public void f2() throws NullPointerException, ArrayIndexOutOfBoundsException {
}
3、自定义异常:
package com.hsf.exception;/*
* @author HSF
* @version 8.0
*/
import javax.sound.midi.Soundbank;
import java.security.PublicKey;
import java.util.Scanner;
public class Exception01 {
public static void main(String[] args) {
int age = 130;
if(!(age >= 18 && age <= 120)) {
// 通过构造器设置错误提示信息
throw new AgeException("年龄要在18~120之间");
}
System.out.println("年龄范围正确");
}
}
class AgeException extends RuntimeException { // 一般继承RuntimeException
// 使用父类的构造器
public AgeException(String message) {
super(message);
}
}
作业:
try里面的异常会等到finally执行完毕后再来执行。
package com.hsf.exception;/*
* @author HSF
* @version 8.0
*/
public class Test {
public static void main(String[] args) {
try {
Except.methodA();
} catch (Exception e) {
}
Except.methodB();
}
}
class Except {
static void methodA() {
try {
System.out.println("进入方法A"); // 第一句输出
throw new RuntimeException("制造异常"); // 第三句输出
} finally {
System.out.println("A方法里面的finally"); // 第二句输出
}
}
static void methodB() {
try {
System.out.println("进入方法B");
return;
} finally {
System.out.println("B方法里面的finally");
}
}
}
作业2:
package com.hsf.exception;/*
* @author HSF
* @version 8.0
*/
import com.sun.xml.internal.ws.api.ha.StickyFeature;
public class Test2 {
public static void main(String[] args) {
try {
if(args.length != 2) {
throw new ArrayIndexOutOfBoundsException("参数个数不对");
}
int n1 = Integer.parseInt(args[0]);
int n2 = Integer.parseInt(args[1]);
double res = cal(n1, n2);
System.out.println(res);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("数组越界");
} catch (NumberFormatException e) {
System.out.println("参数格式不正确");
} catch (ArithmeticException e) {
System.out.println("除0异常");
}
}
public static double cal(double n1, double n2) {
return n1 / n2;
}
}