Java自定义异常类的使用

时间:2021-06-11 00:44:18
功能说明:介绍自定义异常类的使用
class MyException extends Exception
{
public String getMessage()
{
return "Cannot divide by zero!";
}
}
public class Test
{
static int div(int x,int y) throws MyException
{
if(y<0)
{
throw new MyException();
}
return x/y;
}
public static void main(String[] args)
{
try{
div(3,-2);
}catch(MyException me){
System.out.println(me.getMessage());
}
}
}