2020-8-20
Posted cvems700
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2020-8-20相关的知识,希望对你有一定的参考价值。
捕获异常最理想的阶段是在编译阶段,但是错误只有在运行时才会发生
Error:jvm系统内部错误,资源耗尽等严重情况/无法处理,不处理
Exception:因为编程问题造成的一系列错误,需要在编写中处理(除了nullprintexception ,,runtimeexception(运行时异常)及其子类)
异常处理:除了Eroor和 runtimeexception之外的都需要处理
Checked exception(编译异常)
不需要处理的异常:eroor和runtimeexception
Unchecked exception(非检查时异常,运行时异常)
最好在外部声明变量,在try内部赋值,因为try语句块是独立的,不能被外部所用
//常见异常
//1.算数异常java.lang.ArithmeticException
//处理方法:尽量避免
int x= 12;
trySystem.out.println(x/0);
catch(Exception e)
System.out.println(“算数异常”);
//2.类型转换异常:java.lang.ClassCastException
//处理方法:代码问题,修改代码
// Object nowTime =LocalDateTime.now();
// String time =(String) nowTime;
//3.空指针异常nullpointerexception
List l = getList();
System.out.println(l.size());
public static List getList()
List list =new ArrayList();
list.add(1);
return null;
//4.除了runtime exception以外的异常,比如文件异常
File file =new File(“c\\hehe.txt”);
try
file.createNewFile();
catch (IOException e)
// TODO Auto-generated catch block
System.out.println("异常输出");
抛出异常:交给调用者去处理
以上是关于2020-8-20的主要内容,如果未能解决你的问题,请参考以下文章