Java - Difference between throw and throws in Exception handling
2015-07-10 10:57
543 查看
http://javarevisited.blogspot.de/2012/02/difference-between-throw-and-throws-in.html
Sometimes I can't open this page without proper proxy configuration, so I have to copy the content here.
Main difference between throw and throws is in their usage and functionality. where throws is used in method signature to declare Exception possibly thrown by any method, throw is actually used to throw Exception in Java code.
Here is an example of both throw and throws keyword which makes it easy to understand difference between them.
1) You can declare multiple exception thrown by method in throws keyword by separating them in common e.g. throws IOException, ArrayIndexBoundException etc, while you can only throw one instance of exception using throw keyword
e.g. throw new IOException("not able to open connection").
2) throws keyword gives a method flexibility of throwing an Exception rather than handling it. with throws keyword in method signature a method suggesting its caller to prepare for Exception declared in throws clause, specially
in case of checked Exception and provide sufficient handling of them. On the other hand throw keyword transfer control of execution to caller by throwing an instance of Exception.
throw keyword can also be used in place of return as shown in below example:
3) throws keyword cannot be used anywhere except method signature while throw keyword can be used inside method or static initializer block as shown in example.
4) throw keyword can also be used to break a switch statement without using break keyword as shown in below example:
Sometimes I can't open this page without proper proxy configuration, so I have to copy the content here.
Main difference between throw and throws is in their usage and functionality. where throws is used in method signature to declare Exception possibly thrown by any method, throw is actually used to throw Exception in Java code.
Here is an example of both throw and throws keyword which makes it easy to understand difference between them.
public void shutdown() throws IOException{ throw new IOException("Unable to shutdown"); }
1) You can declare multiple exception thrown by method in throws keyword by separating them in common e.g. throws IOException, ArrayIndexBoundException etc, while you can only throw one instance of exception using throw keyword
e.g. throw new IOException("not able to open connection").
2) throws keyword gives a method flexibility of throwing an Exception rather than handling it. with throws keyword in method signature a method suggesting its caller to prepare for Exception declared in throws clause, specially
in case of checked Exception and provide sufficient handling of them. On the other hand throw keyword transfer control of execution to caller by throwing an instance of Exception.
throw keyword can also be used in place of return as shown in below example:
private static boolean shutdown() { throw new UnsupportedOperationException("Not yet implemented"); }
3) throws keyword cannot be used anywhere except method signature while throw keyword can be used inside method or static initializer block as shown in example.
static{ try { throw new Exception("Not able to initialized"); } catch (Exception ex) { Logger.getLogger(ExceptionTest.class.getName()).log(Level.SEVERE, null, ex); } }
4) throw keyword can also be used to break a switch statement without using break keyword as shown in below example:
int number = 5; switch(number){ case 1: throw new RuntimeException("Exception number 1"); case 2: throw new RuntimeException("Exception number 2"); }
相关文章推荐
- 数据可视化 d3js学习之简单的饼图
- 关于IE下jquery.offset发生偏移问题+jquery.qtip偏移问题
- XML与HTML
- 【CSS】CSS画矩形、圆、半圆、弧形、半圆、小三角、疑问框
- 深入了解jsonp解决跨域访问
- Fail safe vs Fail fast
- js 中json字符串转化json对象
- jquery的事件绑定
- html可拖拽元素
- angularjs事件指令
- 【剑指offer】题目20 顺时针打印矩阵
- FastJson的使用
- 原生js 异步请求,responseXML解析
- 移动设备、手机浏览器Javascript滑动事件代码
- JSP内置对象与servlet对应关系
- 不要使用SBJSON(json-framework)
- jQuery.extend 函数详解
- HTML5 五大特性
- ubuntu nodejs + express4.x 安装 测试
- google protocol buffer的原理和使用(四)