编写一个异常类MyException,在编写一个类Student,该类有一个产生异常的方法speak(int m)。
要求参数M大于1000时,方法抛出一个MyException对象,在主类方法中创建Student对象,让该对象调用speak方法
class MyException extends Exception
{
String message;
MyException(int m)
{
message="数字不能大于1000";
}
public String toString()
{
return message;
}
}
class Student
{
public void speak(int m) throws MyException
{
if(m>1000)
{
MyException exception=new MyException(m);
throw exception;
}
System.out.println("I can speak "+m+" words in 3 minutes.");
}
}
public class Test
{
public static void main(String args[])
{
Student a=new Student();
try
{
a.speak(600);
a.speak(1200);
}
catch(MyException e)
{
System.out.println(e.toString());
}
}
}