java学习笔记42
什么是异常
异常指程序运行中出现的不期而至的各种状况,如:文件找不到,网络连接失败,非法参数等
异常发生在程序运行期间,它影响了正常的程序执行流程。
简单分类
检查性异常
最具代表性的检查性异常是用户错误或问题引起的异常,这是程序员无法预见的。例如要打开一个不存在文件时,一个异常就发生了,这些异常在编译时不能被简单地忽略
运行时异常
运行时异常是可能被程序员避免的异常。与检查性异常相反,运行时异常可以在编译时被忽略。
错误error
错误不是异常,而是脱离程序员控制的问题。错误在代码中通常被忽略。例如,当栈溢出时,一个错误就发生了,它们在编译也检查不到的
异常体系结构
java把异常当做对象来处理,并定义一个基类java.lang.Throwable作为所有异常的超类
在java API中已经定义了许多异常类,这些异常类分为两大类,错误error和异常exception
Error
Error类对象由java虚拟机生成并抛出,大多数错误与代码编写者所执行的操作无关。
java虚拟机运行错误,当JVM不再有进行执行操作所需的内存资源时,将出现OutOfMemoryError。这些异常发生时,JVM一般会选择线程终止
还有发生在虚拟机试图执行应用时,如类定义错误(NoClassDefFoundError),链接错误(linkafeerror)。这些错误是不可查的,因为它们在应用程序的控制和处理能力之外,而且绝大多数是程序运行时不允许出现的状况。
Exception
在exception分支中有一个重要的子类RuntimeExceotion(运行时异常)
ArrayIndexOutOfBoundsException(数组下标越界)
NullPointerException(空指针异常)
ArithmeticException(算数异常)
MissingResourceException(丢失资源)
ClassNotFoundException(找不到类)等异常,这些异常是不检查异常,程序中可以选择捕获处理,也可以不处理
区别
Error通常是灾难性的致命的错误,是程序无法控制和处理的,当出现这些异常时,java虚拟机(JVM)一般会选择终止线程;
Excepyion通常情况下是可以被程序处理的,并且在程序中应该尽可能的去处理这些异常
捕获和抛出异常
异常处理五个关键字:try,catch,finally,throw,throws
package exception;
public class Test {
public static void main(String[] args) {
int a = 1;
int b = 0;
System.out.println(a/b);
}
}
输出得到 java.lang.ArithmeticException: 算数异常通过关键字
package exception;
public class Test {
public static void main(String[] args) {
int a = 1;
int b = 0;
try {//try监控区域
System.out.println(a/b);
}catch (ArithmeticException e){//catch捕获异常
System.out.println("程序出现异常,变量b不能为0");
}finally {//处理善后工作
System.out.println("finally");
}
}
}
输出得到;程序出现异常,变量b不能为0
finally
package exception;
public class Test {
public static void main(String[] args) {
int a = 1;
int b = 0;
try {//try监控区域
new Test().a();
}catch (Throwable e){//catch(想要捕获的异常类型)捕获异常
System.out.println("程序出现异常,变量b不能为0");
}finally {//处理善后工作
System.out.println("finally");
}
//finally,可以不要finally
}
public void a(){
b();
}
public void b(){
a();
}
}
输出得到;程序出现异常,变量b不能为0
finally
可以写多个catch,但是最大异常类型要写在最下面
public static void main(String[] args) {
int a = 1;
int b = 0;
try {//try监控区域
System.out.println(a/b);
}catch (Error e){//catch(想要捕获的异常类型)捕获异常
System.out.println("Error");
}catch (Exception e){
System.out.println("Exception");
}catch (Throwable t){
System.out.println("Throwable");
} finally {//处理善后工作
System.out.println("finally");
}、
输出得到
Exception
finally
快捷键 CTRL+ALT+T
package exception;
public class Test2 {
public static void main(String[] args) {
int a = 1;
int b = 0;
//ctrl+alta=t快捷键
try {
System.out.println(a/b);
} catch (Exception e) {
e.printStackTrace();//打印错误的栈信息
} finally {
}
}
}
主动抛出异常(一般在方法中使用)
public static void main(String[] args) {
int a = 1;
int b = 0;
try {//try监控区域
if (b==0){ // throw throws
throw new ArithmeticException(); //主动的抛出异常
}
没有输出情况下还可以抛出异常
package exception;
public class Test {
public static void main(String[] args) {
new Test().test(1,0);
//finally,可以不要finally
}
public void test(int a,int b){
if (b==0){
throw new ArithmeticException();//主动抛出异常
}
}
}
输出Exception in thread "main" java.lang.ArithmeticException
假设这个方法中,处理不了这个异常,方法上抛出异常
package exception;
public class Test {
public static void main(String[] args) {
try {
new Test().test(1,0);
} catch (ArithmeticException e) {
System.out.println("错误");
}
}
//假设这个方法中,处理不了这个异常,方法上抛出异常
public void test(int a,int b) throws ArithmeticException{
if (b==0){
throw new ArithmeticException();//主动抛出异常
}
}
}
输出得到 错误
浙公网安备 33010602011771号