文章目录
Error和Exception的区别
Error
Error类对象由Java虚拟机生成并抛出,大多数错误与代码编写者所执行的
操作无关。
Java虚拟机运行错误(Virtual MachineError),当JVM不再有继续执行操作
所需的内存资源时,将出现OutOfMemoryError。这些异常发生时,Java
虚拟机(JVM)一般会选择线程终止;
还有发生在虚拟机试图执行应用时,如类定义错误(NoClassDefFoundError)、链接错误(LinkageError)。这些错误是不可查的,因为它们在应用程序的控制和处理能力之外,而且绝大多数是程序运行时不允许出现的状况。
Exception
在Exception分支中有一个重要的子类RuntimeException(运行时异常)
- ArrayIndexOutOfBoundsException(数组下标越界)
- NullPointerException(空指针异常)
- ArithmeticException(算术异常)
- MissingResourceException(丢失资源)
- ClassNotFoundException(找不到类)等异常,这些异常是不检查异常,程序中可以选择捕获处理,也可以不处理。
这些异常一般是由程序逻辑错误引起的,程序应该从逻辑角度尽可能避免这类异常的发生;
Error和Exception的区别
Error通常是灾难性的致命的错误,是程序无法控制和处理的,当出现这些异常时,Java虚拟机(JVM)一般会选择终止线程;
Exception通常情况下是可以被程序处理的,并且在程序中应该尽可能的去处理这些异常。
异常的处理机制
- 抛出异常
- 捕获异常
异常处理五个关键字:try catch finally throw throws
finally 可以不要finally, 但假如是IO,资源,则需在finally中关闭
捕获Exception
public class Demo {
public static void main(String[] args){
int a=10;
int b=0;
System.out.println(a/b);
}
}
解决方法:
public class Demo {
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");
}
//finally 可以不要finally, 假如IO,资源,则需在finally中关闭
}
}
运行结果:
程序出现异常,变量b不能为0
finally
Process finished with exit code 0
捕获Error
public class Demo {
public static void main(String[] args){
new Demo().a();
}
public void a(){
b();
}
public void b(){
a();
}
}
解决方法:
public class Demo {
public static void main(String[] args){
try{//try监控区域
new Demo().a();
}catch (Error e){//catch 捕获异常
System.out.println("程序出现错误:StackOverflowError");
}finally {//处理善后工作
System.out.println("finally");
}
}
public void a(){
b();
}
public void b(){
a();
}
}
运行结果:
程序出现错误:StackOverflowError
finally
Process finished with exit code 0
public class Demo {
public static void main(String[] args){
int a=10;
int b=0;
try {
System.out.println(a/b);
}catch (Error e){
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
Process finished with exit code 0
快捷键抛出异常
选中代码:Ctrl+Alt+T
throw/throws 主动抛出异常
public class Demo {
public static void main(String[] args) {
new Demo().test(1,0);
}
public void test(int a,int b){
if (b==0){
//主动抛出异常,一般在方法中使用
throw new ArithmeticException();
}
System.out.println(a/b);
}
}
自定义异常
//自定义异常类
public class MyException extends Exception{
//传递数字>10;
private int detail;
public MyException(int a) {
this.detail = a;
}
//toString:异常的打印信息
@Override
public String toString() {
return "MyException{" + detail +
'}';
}
}
public class Test {
//可能会存在异常的方法
public static void test(int a) throws MyException {
System.out.println("传递的参数为:"+a);
if (a>10){
throw new MyException(a);
}
System.out.println("OK");
}
public static void main(String[] args) {
try {
test(11);
} catch (MyException e) {
//e.printStackTrace();
System.out.println("MyException=>"+e);
}
}
}
//运行结果:
//传递的参数为:9
//OK
//传递的参数为:11
//MyException=>MyException{11}
//e--->toString()
可提供远程搭建运行服务
不会调试运行的同学,你只需打开远程,会帮你搭建调试好一切(JDK、Idea/Eclipse、MySQL、Tomcat、Maven………)