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
    */
  • 相关阅读:
    ◆ C++中通过溢出覆盖虚函数指针列表执行代码
    关于在OnTimer中连续弹出对话框的讨论
    SetTimer
    Windows内核对象
    日志收缩
    暴力求值
    低级问题
    函数限制
    字符串找字段和表
    android错误提示说明汇总
  • 原文地址:https://www.cnblogs.com/taoshihan/p/5509245.html
Copyright © 2011-2022 走看看