编写过程中的错误:
1: MyException()掉括号
2:Exception 拼写错误
3:if(n>0)写成int
import java.util.*;
import java.io.*;
import java.awt.*;
class MyException extends Exception
{
String message;
MyException()
{message="数字不是正数";}
public String toString()
{return message;}
}
class YourException extends Exception
{
String message;
YourException()
{message="数字不是偶数";}
public String toString()
{return message;}
}
class A
{
public void f(int n) throws MyException ,YourException
{
if(n<0)
{throw new MyException();}
if(n%2!=0)
{throw new YourException();}
double number=Math.sqrt(n);
System.out.println(number);
}
}
public class Example
{
public static void main(String args[])
{
A a=new A();
try
{a.f(9);}
catch(MyException e)
{System.out.println(e.toString());}
catch(YourException e)
{System.out.println(e.toString());}
}
}
阅读(376) | 评论(0) | 转发(0) |