对于异常分两种:
1.编译时被检测的异常。
class Demo1
{
int div(int a ,int b)throws Exception
{
if(b==0)
throw new Exception("除数为0");//ArithmeticException---->>Exception,一定要在函数上声明
//同时调用者也要进行处理(要不抛出,要不try处理)
}
}
2.编译时不被检测的异常(运行时异常,RuntimeException以及其子类)
*/
class FushuException extends RuntimeException
{
FushuException (String msg)
{
super(msg);
}
}
class Demo
{
int div (int a,int b)
{
if (b==0)
throw new ArithmeticException("被0除了");
if (b<=0)
throw new FushuException("除数为负数了");
return a/b;
}
}
class ExceptionDemo2
{
public static void main(String[] args)
{
Demo d =new Demo();
int x =d.div(4,-2);
System.out.println("x="+x);
System.out.println("Hello World!");
}
}作者: ifuzhen 时间: 2014-4-5 09:26
很好,学习了作者: 2528870651 时间: 2014-4-5 18:21
学习!!!