java-----错误、异常

java-----错误、异常

1.错误

错误(Error)分为两种情况:

1.JVM系统内部错误,个人无法解决但基本不会出现;

2.资源耗尽等严重情况,编写程序耗费大量内存导致资源耗尽,程序不得不停止运行;

这是属于JVM需要负担的责任这一类异常事件,无法恢复也不可能捕获;

StackOverflowError:栈内存溢出错误:

方法反复调用方法,导致栈内存溢出

public static void main(String[] args) {
		method();
	}
	public static void method(){
		method();
	}

OutOfMemoryError - 内存溢出错误

重复、大量的新建对象,导致内存溢出

public static void main(String[] args) {
		ArrayList<byte[]> list = new ArrayList<>();
		while(true){
			byte[] bs = new byte[1024*1024];
			list.add(bs);
		}
	}

2.异常

异常分为:

1.运行时异常(也叫非受检性异常),编译器不要求强制处置的异常

2.受检异常(也叫一般性异常),编译器要求必须处置的异常

运行时异常 RuntimeException:

编译器强制处置的异常,一般是编程时的逻辑错误。个人可以通过修改代码避免

ArithmeticException 算数异常

算数异常,除数不能为0

System.out.println(15/0);

ArrayIndexOutOfBoundsException 数组下标越界异常

下标越界

int[] is = new int[10];
System.out.println(is[10]);

ClassCastException 类型转换异常

Object 不能转换成 Integer类型

Object obj = new String();
Integer integer = (Integer) obj;
System.out.println(integer);

NullPointerException 空指针异常

字符串为null不能调用.lenhth()

public static void main(String[] args) {
		print(null);
	}
public static void print(String str) {
		System.out.println(str.length());
	}

受检(checked)异常:

指的是程序在运行时由于外界因素造成的一般性异常

程序在编译的时候被编译器检查出来,要求更改的异常

ParseException 解析异常

解析格式不对

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse("2021年9月1日");//解析失败报错
//Date date = sdf.parse("2021-9-1");解析成功
System.out.println(date);

ClassNotFoundException 类未找到异常

找不到这个类

public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
    
		//获取Student类字节码文件对象
		Class<?> c = Class.forName("com.dream.exception01.Student");
		
		//通过字节码文件对象创建Student对象
		Student stu = (Student) c.newInstance();
		System.out.println(stu);
}

FileNotFoundException 文件未找到异常

IOException 输入输出异常

public static void main(String[] args) throws FileNotFoundException,IOException {
		//创建文件对象
		File file = new File("C:\\Users\\XuWenXing\\Desktop\\Xu.txt");
		FileInputStream fis = new FileInputStream(file);//输入流
		System.out.println("文件长度:" + fis.available());
		fis.close();//关流
}

Java处理异常的能力

1.try…catch

try{
可能发生异常的代码
} catch (异常类型 e) {//捕获异常
处理异常的代码
}finally{
必定执行的代码,可以不写
}

处理单个异常

public static void main(String[] args) {
    //处理单个异常
    Scanner scan = new Scanner(System.in);
	String classPath = "C:\\Xu\\wen\\xing";//路径
    try {
        Class<?> c = Class.forName(classPath);
        System.out.println(c);
    } catch (ClassNotFoundException e) {//捕获异常
        e.printStackTrace();//输出异常信息
        System.out.println("捕获到异常!")//发生异常时执行
    } finally {
        scan.close();//不管是否发生异常都会执行
    }
}

处理多个异常

处理多个异常的情况,多个异常的处理方式不一样

注意:先捕获的异常范围不能大于后捕获的异常范围

package com.Xu.exception;
import java.util.Scanner;
public class Test {
	
	public static void main(String[] args) {
        int a=0;
        int b=1;
		String classPath = "C:\\Xu\\wen\\xing";//路径
		try {
			System.out.println(a/b);
			Class<?> c = Class.forName(classPath);
		} catch (ArithmeticException e) {//捕获异常1
			System.out.println("算数异常");
		} catch (ClassNotFoundException e) {//捕获异常2
			System.out.println("类未找到异常");
		}  finally {
			scan.close();//关流
		}
	}
}

处理多个异常

处理多个异常的情况,多个异常的处理方式一样

package com.Xu.exception;
import java.util.Scanner;
public class Test{
	public static void main(String[] args) {
		int a=0;
        int b=1;
		String classPath = "C:\\Xu\\wen\\xing";//路径
		try {
			System.out.println(a/b);
			Class<?> c = Class.forName(classPath);
		} catch (ArithmeticException | ClassNotFoundException e) {//捕获异常
			System.out.println("处理异常");
		}finally {
			scan.close();//关流
		}
	}
}

2.throws - 抛出异常

抛出异常,将异常交给调用方处理,谁调用谁就处理异常

public static void main(String[] args) {

		try {
			method02();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	
	}
	
	private static void method02() throws ClassNotFoundException {//抛出异常
		method01();
	}
	
	public static void method01() throws ClassNotFoundException{//抛出异常
		String classPath = "C:\\Xu\\wen\\xing";//路径
		Class<?> c = Class.forName(classPath);
		System.out.println(c);
		scan.close();
	}

3.throw - 手动抛异常

public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    int a = scan.nextInt();
    int b = scan.nextInt();
    try {
        if(b == 0){
            throw new MyException();//手动抛出异常
        }
    } catch (MyException e) {
        b = 1;
    }
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值