一、异常
了解错误和异常:
Java中的错误 Error:是代表JVM本身的错误,程序员无法通过代码进行处理。Error很少出现,一旦出现就意味着要大换血了。
Java中的异常 Exception:代表Java程序在运行过程中出现了不可预期的错误,影响了代码的正常执行。可以使用Java中的异常处理机制来处理代码,让代码能够正常执行下去。
Java 8 中文版 - 在线API中文手册 - 码工具 (matools.com)
二、Throwable【开发不用】
Java是面向对象开发的,Java中封装好了一个类叫Throwable类,这个类是专门处理错误和异常的类。
学会看官方的API手册:
1.先看这个类是否是接口,类或者抽象类
2.要看继承关系
3.看构造方法(目的是什么,能否实例化)
4.看方法
构造方法:
Throwable()
构造一个新的可抛出的null 作为其详细信息。
Throwable(String message)
构造一个具有指定的详细信息的新的throwable
方法:
String | getMessage() 返回throwable 的详细消息字符串 |
---|---|
void | printStackTrace() 将此throwable 和其追溯打印到标准错误流 |
示例:
package com.qf.wp;
public class a_throwable {
public static void main(String[] args) {
Throwable throwable = new Throwable();
System.out.println(throwable);//java.lang.Throwable
//构造一个新的可抛出的null 作为其详细信息的一个对象
System.out.println(throwable.getMessage());//null
//构造一个具有指定的详细信息的新的throwable
Throwable throwable2 = new Throwable("狗蛋");
System.out.println(throwable2.getMessage());//狗蛋 标准的输出流
throwable2.printStackTrace();//没有返回值是不能sout的 标准的错误流
//java.lang.Throwable: 狗蛋
// at com.qf.wp.a_throwable.main(a_throwable.java:11)
System.out.println("qwer");
}
}
三、异常【重点】
Java中封装好了处理异常的机制
Java异常分为两大类:
(1)编译时异常:在写代码时,编译器会报红
FileNotFoundException//文件未找到异常
SQLException//
ClassNotFoundException
InterruptException
(2)运行时异常:在运行时会出现异常
ArrayIndexOutOfBoundsException//数组下标越界异常
当Java代码出现异常时,就可以使用Java的异常处理机制来处理,若没有异常就正常执行
1.异常的捕捉
在运行过程中,难免会出现异常,这时就可以使用Java中捕捉异常的语法格式来处理异常
语法格式:
try{//尝试
可能出现异常的代码
}catch(异常对象){//抓
//针对上面的异常的处理方案
}
执行流程:如果try里面的代码没有异常,跳过catch,然后接着往下执行。如果try里面有异常,就执行catch后面的代码。
package com.qf.wp;
public class test_try {
public static void main(String[] args) {
test(3,0);
}
public static void test(int a,int b){
int c = 0;
//c = a/b;
/**
* Exception in thread "main" java.lang.ArithmeticException: / by zero
* at com.qf.wp.test_try.test(test_try.java:9)
* at com.qf.wp.test_try.main(test_try.java:5)
*/
try{
c = a/b;
}catch(Exception e){
System.out.println("代码有异常");
System.out.println("除数不能为0");
}
System.out.println("嘻嘻");
System.out.println(c);
}
}
/*
通常执行到有异常的语句(如除数为0)时,系统会报错,并且之后的语句不会执行,但如果将异常语句放到try中,即便出现异常,只要将异常catch住,之后的正常的语句也可以执行下去。
*/
语法格式:
多个异常
try{
可能出现的异常代码
}catch(异常对象1){
}catch(异常对象2){
}
或
try{
可能出现的异常代码
}catch(异常对象1 | 异常对象2){
}
package com.qf.wp;
import jdk.internal.dynalink.beans.StaticClass;
public class test_b_catch {
public static void main(String[] args) {
int[] arr = new int[3];
test(2,0,arr);
}
public static void test(int a,int b,int[] arr){
int c = 0;
try{
c = a/b;//第一个异常语句 除数为0 JVM抛出一个异常ArithmeticException
arr[4] = 20;//第二格异常语句 下标越界 没有执行
}catch(ArithmeticException e){//报错 除数不能为0
System.out.println("除数不能为0");
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("数组下标越界");
}
System.out.println("代码结束");
}
}
/*
如果try语句中有多个异常代码,当执行到第一个异常代码时,就会报相应的异常,后面的异常语句就不再执行。但try-catch语句之后的语句还是能正常执行的。
*/
package com.qf.wp;
public class test_b_catch1 {
public static void main(String[] args) {
int[] arr = new int[3];
test(4,0,arr);
}
public static void test(int a,int b,int[] arr){
int c = 0;
try{
c = a/b;
arr[4] =20;
}catch(ArithmeticException | ArrayIndexOutOfBoundsException e){
System.out.println("除数不能为0或者数组下标越界");
}
System.out.println("代码结束了");
}
}
语法格式:
最终版
try{//尝试
可能出现异常的代码
}catch(异常对象){
//针对于上面异常的处理方案
}finally{
最终执行的代码
}
执行流程:如果try里面的代码没有异常,就跳过catch接着往下执行。如果try里面有异常,就执行catch后面的代码。
finally代码无论有没有异常都要执行。
package com.qf.wp;
public class test_b_catch3 {
public static void main(String[] args) {
int[] arr = new int[3];
test(7,0,arr);
}
public static void test(int a,int b,int arr[]){
int c = 0;
try{
c = a/b;
arr[6] = 39;
}catch(Exception e){
System.out.println("除数不能为0或者数组下标越界");
System.out.println(e.getMessage());// / by zero
}finally{
System.out.println("代码结束了");
}
}
}
2.异常的抛出
为了保证代码编译通过。代码中出现异常的地方进行抛出异常,特别是关于编译时的异常。可以使用异常抛出的语法格式来抛出异常
throws:关键字 名词 告知调用者 此处有异常 要注意 别写错了
package com.qf.wp;
import java.io.FileInputStream;
public class Test_b_throws {
public static void main(String[] args) throws Exception{
System.out.println("你好");
Thread.sleep(1000);//InterruptedException
try {
FileInputStream fis = new FileInputStream("C:\Users\winner\Desktop\Java\power.txt");//文件输入流 其实没有这个文件
}catch(Exception e){
System.out.println(e.getMessage());//标准输出流
e.printStackTrace();//标准错误流
}
System.out.println("嘻嘻");
}
}
/*
在有异常的地方的方法后面throws 异常类
*/
3.throw
抛的动作,可以抛出一个异常对象。自己可以造错!!!
package com.qf.wp;
import java.util.Scanner;
public class Test_b_throw {
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入用户名:");
String username = scanner.next();
if(!username.equals("狗蛋")){
throw new Exception("用户名不存在");
}
System.out.println("请输入密码:");
String keyword = scanner.next();
if(!keyword.equals("123456")){
throw new Exception("密码错误");
}
System.out.println("登陆成功");
}
}
/*
只要throw了就一定要在方法它所在的方法后面加throws,并且throw抛出一个异常后,后面的代码就不再执行了
*/
4.自定义异常
开发中会遇到很多异常,但Java中提供的异常不足以描述。可以试着自己造异常
package com.qf.wp;
class SingleException extends Exception{//模仿着写一个自定义异常类
public SingleException(){//无参构造
}
public SingleException(String message){//有参构造
super(message);
}
}
public class Test_b_throw1 {
public static void main(String[] args) throws Exception{
buy(true);
}
public static void buy(boolean isSingle) throws Exception{
if(isSingle){
throw new SingleException("单身异常");
}
System.out.println("情侣买一送一");
}
}
完结,撒花~