ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

学习010---异常Exception

2021-07-30 11:05:59  阅读:161  来源: 互联网

标签:--- Exception System 010 catch finally println public out


注:本系列学习自狂神说(Java):

点击跳转至狂神说视频(bilbili)

点击跳转至狂神说网页

当前的目标是建造自己的网页!

俺的博客:startsmaple

目录

Exception

异常指程序在运行中出现的不期而至的各种情况,如:文件找不到、网络连接失败、非法参数等

异常发生在程序运行期间,他影响了正常的程序执行流程

简单分类

  • 检查性异常:最具代表的检查性异常是用户错误或问题引起的异常,这是程序员无法预见的,例如要打开一个不存在文件时,一个异常就发生了,这些异常在编译时不能被简单的忽略。

    ​ 测试:在某些特定环境下测bug

  • 运行时异常:运行时异常是可能被程序员避免的异常,与检查性异常相反,运行时异常可以在编译的时候被忽略。(最容易避免的,一般在运行时才会出现

  • 错误:错误不是异常,而是脱离程序员控制的原因错误,在代码中经常被忽略,例如栈在溢出时一个错误就发生了,他们在编译也查不到的

结构

image

Error

  • Error类对象由Java虚拟机生成并抛出,大多数与代码编写者所执行的操作无关

image

Exception

  • 编译错误

image

异常处理机制

try catch finally throw throws

package com.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 {//finally处理善后工作
            System.out.println("finally");
        }
    }
}
  • try catch是必须要有的,而finally可以没有

  • finally:用来IO流、资源、关闭程序

  • catch()里面填想要捕获的异常类型,最高的就是Throwable

package com.exception;

public class Test {
    public static void main(String[] args) {
        int a = 1;
        int b = 0;
        try{//try监控区域
            new Test().a();
        }catch (ArithmeticException e){//catch捕获异常
            System.out.println("程序出现异常,变量b不能为0");
        }finally {//finally处理善后工作
            System.out.println("finally");
        }
    }
    public void a(){
        b();
    }
    public void b(){
        a();
    }
}

没有捕获到,但仍然执行善后

程序在执行完finally之后报错

用最大的来捕获

package com.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 {//finally处理善后工作
            System.out.println("finally");
        }
    }
    public void a(){
        b();
    }
    public void b(){
        a();
    }
}

层层递进,捕获多个异常

package com.exception;

public class Test {
    public static void main(String[] args) {
        int a = 1;
        int b = 0;
        try{//try监控区域
            new Test().a();
        }catch(Error e){
            System.out.println("Error");
        }catch(Exception e){
            System.out.println("Exception");
        }catch (Throwable e){
            System.out.println("Throwable");
        }finally {
            System.out.println("finally");
        }
    }
}

out:
	Exception
    finally
  • 注意

catch捕获的异常需要从小到大。

从大到小会编译错误

package com.exception;

public class Test {
    public static void main(String[] args) {
        try{
            new Test().a();
        }catch (Throwable e){
            System.out.println("Throwable");
        }catch(Exception e){
            System.out.println("Exception");
        }finally {
            System.out.println("finally");
        }
    }
}
  • catch捕获到就执行finally

  • 快捷键Ctrl+Alt+T:自动用代码块包裹选中内容

  • e.printStackTrace():打印错误的栈信息,也就是红色的那一坨

  • System.exit(0):结束

主动抛出异常throw

throw 和 throws不一样

package com.exception;

public class Test {
    public static void main(String[] args) {
        new Test().test(1,0);
    }
    public void test(int a,int b){
        if(b == 0){
            throw new ArithmeticException();//主动抛出异常
        }
        System.out.println("a/b");
    }
}

主动抛出异常,程序终止,

出行一坨红色的异常,并没有继续执行下面的内容

方法内部处理不了的异常throws

package com.exception;

public class Test {
    public static void main(String[] args) {
        try {
            new Test().test(1,0);
        } catch (ArithmeticException e) {
            System.out.println("Ari");
        } finally {
        }
        System.out.println("???????????");
    }
    public void test(int a,int b) throws ArithmeticException{
        if(b == 0){
            throw new ArithmeticException();
        }
        System.out.println("a/b");
    }
}

out:
	Ari
    ???????????????

解析(待修正

  1. b==0,抛出异常throw ArithmeticException()
  2. 方法抛出异常throws ArithmeticException()
  3. catch捕获到异常ArithmeticException()
  4. 继续执行

自定义异常

package com.exception.demo02;

public class MyException extends Exception{
    //传递数字 大于10 抛出异常
    private int detail;

    public MyException(int a){
        this.detail = a;
    }
    //toString:异常的打印信息

    @Override
    public String toString() {
        return "MyException{" +
                "detail=" + detail +
                '}';
    }
}
package com.exception.demo02;

public class Test {
    //可能出现异常的方法
    static void test(int a) throws MyException {
        if(a>10){
            throw new MyException(a);
        }else{
            System.out.println("ojbk");
        }
    }

    public static void main(String[] args) {
        try {
            test(11);
        } catch (MyException e) {
            System.out.println("MyException =>  " + e);
        }
    }
}

image

标签:---,Exception,System,010,catch,finally,println,public,out
来源: https://www.cnblogs.com/starsmaple/p/15078587.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有