标题: 异常 问题 头晕啊!!! [打印本页] 作者: 文密 时间: 2012-4-8 19:31 标题: 异常 问题 头晕啊!!! class FuShuException extends Exception
{
FuShuException()
{
super();
}
FuShuException( String msg)
{
super(msg);
}
}
class Demo
{
int div(int a , int b)throws FuShuException
{
if (b<0)
throw new FuShuException();//手动通过throw关键字抛出一个自定义异常对象"出现了除数为负数"
return a/b;
}
}
class ExceptionDemo3
{
public static void main(String[] args)
{
Demo d = new Demo();
try
{
int x = d.div(4,-1);
System.out.println("x="+x);
}
catch (FuShuException e)
{
System.out.println(e.toString());
System.out.println("除数为负数");
}
System.out.println("over");
}
}
--------------------------------------------------------------------------------
class Demo
{
int div(int a , int b)//throws
{
if(b==0)
throw new ArithmeticException("被零除了");
return a/b;
}
}
class ExceptionDemo4
{
public static void main(String[] args)
{
Demo d = new Demo();
int x = d.div(4,0);
System.out.println("x="+x);
System.out.println("over");