inheriting.java

来自「一个十分好的java基础学习的课件」· Java 代码 · 共 35 行

JAVA
35
字号
//Inheriting your own exceptions
class MyException2 extends Exception
{   public MyException2( ) { }
    public MyException2(String msg)
    {   super(msg);   }
    public MyException2(String msg,int x)
    {   super(msg);
        i=x;
    }
    public int val( ) {   return i;   }
    private int i;
}

public class Inheriting
{   public static void f( ) throws MyException2
    {   System.out.println("Throwing MyException2 from f( )");
        throw new MyException2( );
    }
    public static void g( ) throws MyException2
    {   System.out.println("Throwing MyException2 from g( )");
        throw new MyException2("起源于 g( ) 的异常",47);
    }
    public static void main(String args[ ])
    {   try
        {   f( );   }
        catch(MyException2 e)
        {   e.printStackTrace( );   }
           try
        {   g( );   }
        catch(MyException2 e)
        {   e.printStackTrace( );
            System.out.println("i = "+e.val( ));
        }
   }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?