throw 和throws 的区别

  • 区别一:使用位置不同
    throws使用在函数上,在定义函数的小括号后面接throws;
    throw使用在函数内;
  • 区别二:
    throws后跟异常类,可以跟多个,用逗号隔开;
    throw 后跟异常对象。
public int div(int a,int b)  throws ZeroException{
	if(b==0){
		throw new ZeroException("出现了除数为0的情况",b);
	}
	else{
		return a/b;
	}
}

博主:fager
2019-03-25
21:36

猜你喜欢

转载自blog.csdn.net/qq_41953807/article/details/88806619
今日推荐