zoukankan      html  css  js  c++  java
  • [javaSE] 异常捕获

    异常:程序在运行时出现的不正常现象

    Throwable

    |——Error

    |——Exception

    严重级别:Error类和Exception

    异常的处理:try{}catch{}finally{}

    public class VariableDemo {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            try {
                System.out.println(1/0);
                System.out.println("此处不会执行");
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("异常捕获");
            }finally{
                System.out.println("异常finally");
            }
            
            /**
             * 输出:
             * java.lang.ArithmeticException: / by zero
                    at VariableDemo.main(VariableDemo.java:9)
                异常捕获
                异常finally
             */
        }
    
    }

    PHP中的异常捕获,必须手动抛异常,并且finally是在PHP5.5以上才有

    <?php
    
    function getNum($a){
        if(!$a){
            throw new Exception("Division by zero.");
        }
        return 10/$a;
    }
    try{
        echo getNum(0);
    
    }catch(Exception $e){
        echo $e->getMessage();
        echo "异常捕获";
    } finally {
        echo "异常finally";
    }
    
    /**
    *输出
    *Division by zero.   异常捕获   异常finally
    */
  • 相关阅读:
    docker网络
    docker-registry搭建
    JDK1.8源码安装
    docker-compose应用
    docker-compose介绍
    docker commit
    CMD/ENTROYPOINT区别
    第一个dockerfile
    SpringBoot整合Mybatis对单表的增、删、改、查操作
    向上取整、向下取整
  • 原文地址:https://www.cnblogs.com/taoshihan/p/5509245.html
Copyright © 2011-2022 走看看