teexption.java

来自「用java编写的一些初级代码」· Java 代码 · 共 47 行

JAVA
47
字号
class DevideByExcep extends Exception
{
    int devisor;
    public DevideByExcep(String msg ,int devisor)
    {
	super (msg);
	this.devisor=devisor;
    }
    public int getDevisor()
    {
	return devisor;
    }
}
public class TeExption
{
    public static void main (String[] args)
    {
	try
	{
	    int result= new Test().devide(3,-12);
	    System.out.println("the result is "+result);
	}
	catch (DevideByExcep e)
	{
	    System.out.println(e.getMessage());
	    System.out.println("the devisor is "+e.getDevisor());
	}
	catch (Exception e)
	{
	    System.out.println(e.getMessage());
	    e.printStackTrace();
	}
	System.out.println("pro is runing");
    }
}
class Test
{
    public int devide(int x,int y)throws Exception
    {
	if(y < 0)
	    throw new DevideByExcep("被除数为负",y);
	int result=x/y;
	return result;
    }
}

⌨️ 快捷键说明

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