面向对象-异常:RuntimeException

Exceptoin 中有一个特殊的子类异常  RuntimeException
如果在函数内容抛出该异常,函数上可以不用声明,编译一样通过。
如果在函数上声明了该异常,调用者可以不用进行处理,编译一样通过。

自定义异常时:如果该异常的发生无法在继续进行运算,就让自定义异常继承RuntimeException。


对于异常分两种:
1、编译时被检测的异常。

2、编译时不被检测的异常(运行时异常:RuntimeException以及其子类)

代码:

class Demo
{
	int div(int a,int b)
	{
		if(b<0)
			throw new ArithmeticException("出现负数情况");
		return a/b;
	}
}

public class code
{
    public static void main(String[] args) {
        Demo d = new Demo();
		int x = d.div(4,-1);
		System.out.println(x);
    }
}

你可能感兴趣的:(Java-面向对象,Java学习)