throw与throws的区别

依然范特西╮ 提交于 2019-11-28 22:41:19

throw与throws的区别

1.throw是在方法内部的代码块中,手动异常抛出
2.throws是在方法定义时用

举例如下

1、手动抛出–throw

public class Hello {
	
	public static void main(String[] args) {
	
		try {
			throw new Exception("手动抛出的异常");
		}catch(Exception e) {
			e.printStackTrace();
		}
		
    }
}

2、方法定义时–throws

class Text{
	public static int div(int x,int y) throws Exception{
		return x/y;
	}
	
}

public class Hello {
	
	public static void main(String[] args) {
		try {
			System.out.println(Text.div(12, 0));
		}catch(Exception e) {
			e.printStackTrace();
		}
	
    }
}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!