⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 try7.java

📁 Java Classic Examples是我买的两本书:《JAVA经典实例》和《java入门经典源代码》里边附送光盘里带的源码
💻 JAVA
字号:
class OverflowException extends Exception      //自定义异常类
{
    public void printMsg()
    {
        System.out.println("exception: "+this.getMessage());
        this.printStackTrace();
        System.exit(0);
    }
}
public class Try7
{
    public void calc(byte k) throws OverflowException //抛出异常
    {
        byte  y=1,i=1;
        System.out.print(k+"!=");
        for (i=1;i<=k;i++)
        {
            if(y>Byte.MAX_VALUE/i)
                throw new OverflowException();
            else
                y = (byte)(y * i);
        }
        System.out.println(y);
    }
    public void run(byte k)                     //捕获并处理异常
    {
        try
        {
            calc(k);
        }
        catch(OverflowException e)
        {
            e.printMsg();
        }
    }
    public static void main (String args[])
    {
        Try7 a = new Try7();
        for (byte i=1;i<10;i++)
            a.run(i);
    }
}

⌨️ 快捷键说明

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