zoukankan      html  css  js  c++  java
  • java异常之后运行

    //代码1
    public static void test() throws Exception  {
    
        throw new Exception("参数越界"); 
        System.out.println("异常后"); //编译错误,「无法访问的语句」
    }
    //代码2
    try{
        throw new Exception("参数越界"); 
    }catch(Exception e) {
        e.printStackTrace();
    }
    System.out.println("异常后");//可以执行  
    //代码3
    if(true) {
        throw new Exception("参数越界"); 
    }
    System.out.println("异常后"); //抛出异常,不会执行
    
    
    //代码4
    public static void main(String[] args) {
            int i =10 + 0;
            tt();
            System.out.println("fefegeg");//不可以执行
    
        }
    
        public static void tt(){
    throw new Exception("参数越界"); 
     }
    
    
    //代码5
    public static void main(String[] args) {
            int i =10 + 0;
            tt();
            System.out.println("fefegeg");//可以执行
    
        }
    
        public static void tt(){
            try{
    throw new Exception("参数越界"); 
     }catch (Exception e){ e.printStackTrace(); } }

    1.若一段代码前有异常抛出,并且这个异常没有被捕获,这段代码将产生编译时错误「无法访问的语句」。如代码1

    2.若一段代码前有异常抛出,并且这个异常被try...catch所捕获,若此时catch语句中没有抛出新的异常,则这段代码能够被执行,否则,同第1条。如代码2

    3.若在一个条件语句中抛出异常,则程序能被编译,但后面的语句不会被执行。如代码3

    4.调用方法发生异常,本方法之后无法执行。如代码4

    5.调用方法发生异常并捕获,本方法后可以继续执行。如代码5

  • 相关阅读:
    HUSTOJ:Transit Tree Path
    HUSTOJ:5500 && 洛谷:P1412:经营与开发
    hdu:2036.改革春风吹满地
    hdu:2030.汉字统计
    Leetcode:338. Bit位计数
    【学习笔记】圆方树(CF487E Tourists)
    BZOJ3238 [Ahoi2013]差异
    CF 187D BRT Contract
    CF 36E Two Paths
    CF 49E Common ancestor
  • 原文地址:https://www.cnblogs.com/chong-zuo3322/p/13062492.html
Copyright © 2011-2022 走看看