/*
Exception中有一个特殊的子类异常RuntimeException 运行时异常,
如果在函数内部抛出该异常,函数上可以不用声明,编译一样通过。
若果在函数上声明了该异常,调用着可以不用进行处理,编译一样通过
之所以不用在函数声明,是因为不需要让调用者处理,
当该异常发生,希望程序停止,因为在运行时,出现了无法继续运算的情况,希望停止程序后,
对代码进行修正
自定义异常时,如果该异常的发生,无法再继续进行运算,
就让自定义异常继承RuntimeException。
对于异常分两种:
1、编译时被检测的异常,-------里面抛出,外面必须标识
2、编译时不被检测的异常(运行时异常,RuntimeException及其子类)-----里面抛出异常,外面不用标识
*/
class FuShuException1 extends RuntimeException
{
FuShuException1(String msg)
{
super(msg);
}
}
class Demo3
{
int div(int a,int b)//throws ArithmeticException 可以不声明
{
if(b<0)
{
throw new FuShuException1("出现除数为负数!");
}
if (b==0)
throw new ArithmeticException("被零除了!");
return a/b;
}
}
class ExceptionDemo3
{
public static void main(String[] args)
{
Demo3 d = new Demo3();
int x = d.div(4,-1);
System.out.println("x="+x);
System.out.println("Over");
}
}
/*
class Person
{
public void checkName(String name)//不用声明,出错让函数停止,用户进行修改
{
// if (name.equals("lisi")) //NullPointerException
if("lisi".equals(name))//if(name!=null && name.equals("lisi"))
System.out.println("YES");
else
System.out.println("NO");
}
}
main()
{
Person person =new Person();
person.checkName(null);
}
*/