文章目录
什么是异常
异常就是在编译或执行的过程中可能会出现的问题,语法错误并不属于异常
异常一旦出现,如果没有提前处理,程序就会退出JVM虚拟机
异常在java.lang报下有一个单独的异常类:Exception。
异常的分类
RunTimeException及其子类
运行时异常,在编译阶段不会报错
简单来说这类异常一般时程序自己不严谨或能力不足导致的错误
几种常见的运行时异常
public static void main(String[] args) {
//ArrayIndexOutOfBoundsException,数组索引越界异常
int []a=new int[3];
//System.out.println(a[3]);
//NullPointerException,空指针异常,直接输出没问题,但是调用方法就会报错
String s=null;
System.out.println(s);
//System.out.println(s.length());
//ClassCastException,类型转换异常
Object x=123456;
//String y=(String) x;
//ArithmeticException,数学操作异常
//int c=10/0;
//NumberFormatException,数字转换异常
String number="23bbb";
Integer num=Integer.valueOf(number);
}
除RunTimeException及其子类之外的所有异常
编译时异常,编译时就会报错,不处理,程序就无法运行
担心程序的能力不行,特意提醒有可能会出错
异常的默认处理机制
1.在出现异常的地方自动创建一个异常对象:ArithmeticException
2.再将异常对象抛给调用者,最终抛给JVM虚拟机
3.虚拟机收到异常对象后,在控制台输出异常栈信息
4.在代码出现问题的地方直接终止程序
编译时异常的处理形式
thows
就是直接将内部出现的异常抛给调用者
public static void main(String[] args) throws FileNotFoundException {
System.out.println("程序开始");
show();
System.out.println("程序结束");
}
public static void show() throws FileNotFoundException {
InputStream study=new FileInputStream("D:/学习资料");
}
这样虽然在编译阶段不会报错了,但是如果在运行过程中程序有问题,程序仍旧会终止
Tips
在抛出异常时在thows方法后一般直接用Exception,好处时在面对有多个异常时,不需要一个一个的抛出
public static void main(String[] args) throws Exception {
System.out.println("程序开始");
show();
System.out.println("程序结束");
}
public static void show() throws Exception {
InputStream study=new FileInputStream("D:/学习资料");
}
try…catch…
监视捕获异常,可以在方法的内部直接将异常捕获处理
这样即使在执行过程中出错,程序不会终止,仍会继续运行
public static void main(String[] args){
System.out.println("程序开始");
show();
System.out.println("程序结束");
}
public static void show(){
try {
InputStream study=new FileInputStream("D:/学习资料");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
还可以在catch里面自定义内容
public static void main(String[] args){
System.out.println("程序开始");
show();
System.out.println("程序结束");
}
public static void show(){
try {
InputStream study=new FileInputStream("D:/学习资料");
System.out.println(6);
} catch (Exception e) {
e.printStackTrace();
System.out.println("没有找到呢");
}
}
但是在try里面有问题的代码后面依旧是不会执行
上述二者方法结合
即在底层方法内部先将异常抛出,在最外层中集中捕获处理
public static void main(String[] args){
System.out.println("程序开始");
try {
show();
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("没找到呢");
}
System.out.println("程序结束");
}
public static void show() throws FileNotFoundException {
InputStream study=new FileInputStream("D:/学习资料");
}
运行时异常的处理形式
编译阶段不会报错,不需要处理
只需要在最外层捕获处理即可
public static void main(String[] args) {
System.out.println("程序开始");
try {
chu(10,0);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("程序结束");
}
public static void chu(int a,int b){
System.out.println(a/b);
}
自定义异常
可以使用异常的机制管理业务提醒程序员注意
出现bug可以用异常的形式清晰的指出出错的地方
自定义编译时异常
定义一个异常类继承Exception
重写构造器
在出现异常的地方用throw new 抛出
public static void main(String[] args) {
System.out.println("程序开始");
try {
check(-10);
} catch (IlleagalExcept e) {
e.printStackTrace();
System.out.println("输入不合法" + e);
}
System.out.println("程序结束");
}
public static void check(int weight) throws IlleagalExcept {
if(weight<0||weight>100){
throw new IlleagalExcept(weight+" is illeagal");
}
else{
System.out.println("体重输入正常");
}
}
自定义运行时异常
定义一个异常类继承RuntimeException
重写构造器
在出现异常的地方用throw new 抛出
public static void main(String[] args) {
System.out.println("程序开始");
/*try {
check(-10);
} catch (IlleagalRuntimeException e) {
e.printStackTrace();
System.out.println("输入不合法" + e);
}*/
check(-10);
System.out.println("程序结束");
}
public static void check(int weight) throws IlleagalRuntimeException {
if(weight<0||weight>100){
throw new IlleagalRuntimeException(weight+" is illeagal");
}
else{
System.out.println("体重输入正常");
}
}
编译阶段不报错