Java异常

目录

1.Java中的异常

2.Throwable类

3.错误和异常

 3.1异常

3.1.1 异常的捕捉

3.1.2 异常的抛出

 3.1.3 自定义异常


1.Java中的异常

在开发过程中,程序在自上而下执行的时候,发生了不可预期的事件,这个事件阻止程序的运行。这就是异常。

 例如:

数组下标越界 ArrayIndexOutOfBoundException

类转换异常 ClassCastException


Java给咱们封装很多的异常类,并且提供很优秀的异常处理机制。其中Java提供了一个类Throwable

2.Throwable类

构造方法:

Throwable()  构造一个新的可抛出的 null作为其详细信息。 Throwable(String message)  构造一个具有指定的详细消息的新的throwable。

成员方法:

String getMessage()返回此throwable的详细消息字符串。 void printStackTrace()将此throwable和 其追溯打印到标准错误流。 String toString()返回此可抛出的简短描述。

public class Demo1 {
	public static void main(String[] args) {
		System.out.println("Hello");
		System.err.println("Word");
		Throwable throwable = new Throwable();
		System.out.println(throwable.getMessage());
		Throwable throwable2 = new Throwable("你好");
		System.out.println(throwable2.getMessage());
		/**
		 * java.lang.Throwable: 你好
	at com.qfedu.b_trhowable.Demo1.main(Demo1.java:7)

		 */
		throwable2.printStackTrace();
		//java.lang.Throwable: 你好
		//告知了这个错误信息
		System.out.println(throwable2.toString());
	}

}

3.错误和异常

Throwable 下面有两个子类 一个叫Error, 一个叫Exception

Error:是代表JVM本身的错误,咱们程序员是通过代码解决不了的。

Exception: 异常,代表程序在运行过程中,发生了不可预期的事件。可以使用Java找出来,让他继续执行下去。

异常分为两种:

 编译时异常:

FileNotFoundException

SQLException

ClassNotFoundException

InterruptException

运行时异常:

数组下标越界 ArrayIndexOutOfBoundException

类转换异常 ClassCastException

空指针异常 NullPointerException

 3.1异常

代码有可能会出现异常。Java给咱们提供了两种解决方案

1.异常的捕捉

2.异常的抛出


3.1.1 异常的捕捉

在程序运行过程中,代码难免有可能会遇到异常。如果没有异常,代码正常执行。

如果有异常,就捕捉异常

语法格式:

try {
    有可能出现异常的代码
} catch (异常对象) {
    //针对于面异常的处理方案
}

 案例1:

public class Demo2 {

	public static void main(String[] args) {
		test(3, 0);
	}
	public static void test (int a, int b) {
		int ret = 0;
		try {
			//有可能个出现异常的代码
			 ret = a / b;
		} catch (ArithmeticException e) {
			System.out.println("123");
			//打印错误信息
			System.out.println(e.getMessage());
		}
		//即使代码有异常,通过捕捉以后。是不会影响程序的接着执行的代码的
		System.out.println(ret);
		System.out.println( 4 + 4);
	}
}

 案例2:

public class Demo3 {
	public static void main(String[] args) {
		int[] arr = new int[2];//数组的容量是2
		test(1, 1, arr);
	}
	public static void test (int a, int b, int[] arr) {
		int ret = 0;
		try {
			ret = a / b;//有可能出现的异常的代码
			
			arr[2] = 89;//这个会有异常
			//jvm造出来哪个异常对象,就去catch 到哪个异常然后去执行具体的catch
			
		} catch (ArithmeticException e) {
			System.out.println("123");
			System.out.println(e.getMessage());
		} catch (ArrayIndexOutOfBoundsException e) {
			System.out.println("456");
			System.out.println(e.getMessage());
		}
		
		System.out.println(4 + 8);
	}

}

 案例2 改进版:

public class Demo4 {
	public static void main(String[] args) {
		int[] arr = new int[2];//数组的容量是2
		test(1, 1, arr);
	}
	public static void test (int a, int b, int[] arr) {
		int ret = 0;
		try {
			ret = a / b;//有可能出现的异常的代码
			
			arr[2] = 89;//这个会有异常
			//jvm造出来哪个异常对象,就去catch 到哪个异常然后去执行具体的catch
			
		} catch (ArithmeticException | ArrayIndexOutOfBoundsException e) {
			System.out.println("123");
			System.out.println(e.getMessage());
		} 
		
		System.out.println(4 + 8);
	}

}

 因为Exception类是所有异常的父类,所以我们可以继续对案例2进行改进,

案例2 最终版:

public class Demo5 {
	public static void main(String[] args) {
		int[] arr = new int[2];//数组的容量是2
		test(1, 0, arr);
	}
	public static void test (int a, int b, int[] arr) {
		int ret = 0;
		try {
			ret = a / b;//有可能出现的异常的代码
			
			arr[2] = 89;//这个会有异常
			//jvm造出来哪个异常对象,就去catch 到哪个异常然后去执行具体的catch
			
		} catch (Exception e) {
			System.out.println("123");
			System.out.println(e.getMessage());
		} 
		
		System.out.println(4 + 8);
	}

}

finally的用法 

 try {

    } catch {

    } finally {

    }

public class Demo5 {
	public static void main(String[] args) {
		int[] arr = new int[2];//数组的容量是2
		test(1, 0, arr);
		
	}
	public static void test (int a, int b, int[] arr) {
		int ret = 0;
		try {
			ret = a / b;//有可能出现的异常的代码
			
			arr[2] = 89;//这个会有异常
			//jvm造出来哪个异常对象,就去catch 到哪个异常然后去执行具体的catch
			
		} catch (Exception e) {
			System.out.println("123");
			System.out.println(e.getMessage());
		} finally {
			//无论有没有异常,最终都要执行的
			System.out.println(4 + 8);
		}
		
	}

}

3.1.2 异常的抛出

在代码出现异常的地方进行异常的抛出

如果异常的抛出的话,一旦发生异常,从出现异常的地方会终止代码

使用两个关键字:

throw: 在方法中抛出一个异常。自己造一个错

throws: 在方法的声明处书写,告知当前调用者,此处有异常。要小心

import java.io.FileNotFoundException;

public class Demo8 {
	public static void main(String[] args) throws Exception{
		test(0);
		Thread.sleep(1000);
	}
	public static void test (int a) throws FileNotFoundException{
		if (a == 0) {
			//编译时异常
			throw new FileNotFoundException();
		}
		System.out.println("jvm Hello");
	}

}

 3.1.3 自定义异常

Java给咱们提供了很多的异常对象,但是有的时候还是满足不了现实生活的需求,我们可以自己造异常对象。

需要继承Exception

import java.util.Scanner;

public class Demo11 {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入一个整数:");
		int score = scanner.nextInt();
		try {
			test(score);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
	public static void test (int score) throws Exception {
		if (score > 100 || score < 0) {
			throw  new Exception("输入的整数有误的。。。");
		}
		if (score >= 90 && score <= 100) {
			System.out.println("优秀");
		} else if (score >= 80) {
			System.out.println("良好");
		} else if (score >= 70) {
			System.out.println("及格");
		} else {
			System.out.println("叫家长。。。");
		}
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值