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" );
    }
    }

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

  • 相关阅读:
    C语言C++编程学习:排序原理分析
    Ubuntu下开机进程管理工具sysvrcconf
    康托展开
    USACO 3.2 Magic Squares题解
    『转』ubuntu的电池管理
    poj1061青蛙的约会解题报告
    Dancing Links
    【转】史上最强vim配置文件vimrc
    四柱汉诺塔
    又遇欧拉 转载之
  • 原文地址:https://www.cnblogs.com/092e/p/14146502.html
Copyright © 2011-2022 走看看