testexception.java

来自「我的一个JAVA作业」· Java 代码 · 共 44 行

JAVA
44
字号
class MyException extends Exception
{
	int r;
	public MyException(String msg,int r)
	{
		super(msg);
		this.r=r;
	}
	public int getR()
	{
	return r;
	}
}
		
class Test
{   int r;
	public double area(int r)throws ArithmeticException,MyException
	{   
	    this.r=r;
		if(r<0)
		throw new MyException("半径为负数了",r);
		double result=3.14*r*r;;
	return result;		
	}	
}
public class TestException
{
 public static void main(String args[])
 {
try{double result=new Test().area(-3);
			System.out.println("the result is"+result);
			}
	catch(MyException e1){
	
			System.out.println("is MyException");
			System.out.println(e1.getMessage());
			System.out.println("the R is "+e1.getR());
			}
	finally{
	System.out.println("program is running into finally");
	    }	
	}	
}

⌨️ 快捷键说明

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