zoukankan      html  css  js  c++  java
  • 异常处理

    11.11

    今天练溪的是异常处理连:

    代码部分:

    /**
    * 自定义的异常类
    * @author JinXuLiang
    *
    */
    class MyException extends Exception
    {
    public MyException(String Message)
    {
    super(Message);
    }
    public MyException(String message, Throwable cause)
    {
    super(message, cause);
    }
    public MyException( Throwable cause)
    {
    super(cause);
    }

    }

    public class ExceptionLinkInRealWorld
    {
    public static void main( String args[] )
    {
    try
    {
    throwExceptionMethod(); //有可能抛出异常的方法调用
    }
    catch ( MyException e )
    {
    System.err.println( e.getMessage() );
    System.err.println(e.getCause().getMessage());
    }
    catch ( Exception e )
    {
    System.err.println( "Exception handled in main" );
    }
    doesNotThrowException(); //不抛出异常的方法调用
    }

    public static void throwExceptionMethod() throws MyException
    {

    try
    {
    System.out.println( "Method throwException" );

    throw new Exception("系统运行时引发的特定的异常"); // 产生了一个特定的异常
    }
    catch( Exception e )
    {
    System.err.println(
    "Exception handled in method throwException" );
    //转换为一个自定义异常,再抛出
    throw new MyException("在方法执行时出现异常",e);

    }
    finally
    {
    System.err.println(
    "Finally executed in throwException" );
    }

    // any code here would not be reached
    }

    public static void doesNotThrowException()
    {
    try
    {
    System.out.println( "Method doesNotThrowException" );
    }
    catch( Exception e )
    {
    System.err.println( e.toString() );
    }
    finally
    {
    System.err.println(
    "Finally executed in doesNotThrowException" );
    }

    System.out.println(
    "End of method doesNotThrowException" );
    }
    }

     这就是典型的异常处理代码模板。

  • 相关阅读:
    JS定义一个立即执行的可重用函数
    Git常用命令速记与入门
    设计的一些kubernetes面试题
    运维知识各种链接
    php7.2安装smbclient扩展
    logrotate自定义切割时间的一些坑
    【转】日志收集工具scribe
    ELK日志报警插件ElastAlert并配置钉钉报警
    consul-server集群搭建
    加油,骚年
  • 原文地址:https://www.cnblogs.com/092e/p/14146502.html
Copyright © 2011-2022 走看看