首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Java SE 七 Exception的使用

2012-08-31 
Java SE 7 Exception的使用Java SE 7 Exception的使用简介:Multi-CatchExceptionspublic class ExampleExc

Java SE 7 Exception的使用

Java SE 7 Exception的使用

简介:

Multi-CatchExceptions

public class ExampleExceptionHandling{ public static void main( String[] args ) { try { URL url = new URL("http://www.yoursimpledate.server/"); BufferedReader reader = new BufferedReader(newInputStreamReader(url.openStream())); String line = reader.readLine(); SimpleDateFormat format = new SimpleDateFormat("MM/DD/YY"); Date date = format.parse(line); } catch(ParseException exception) { // handle passing in the wrong type of URL. } catch(IOException exception) { // handle I/O problems. } catch(ParseException exception) { // handle date parse problems. } }}?

?

?

public class ExampleExceptionHandlingLazy{   public static void main( String[] args )   {         try {                  URL url = new URL("http://www.yoursimpledate.server/");                  BufferedReader reader = new             BufferedReader(new InputStreamReader(url.openStream()));                  String line = reader.readLine();                  SimpleDateFormat format = new SimpleDateFormat("MM/DD/YY");                  Date date = format.parse(line);         }         catch(Exception exception) {                  // I am an inexperienced or lazy programmer here.         }   }}
?

?

在例2代码示例中最大的毛病就是会产生不可预料的副作用,任何代码在try代码块可能产生异常,所有异常将全部由catch子句所处理。如果一个异常既不是ParseException也不是IOException(例如:SecurityException),此异常仍然会被捕获,但是上游用户去不知道真正发生了什么?这种吞并式的异常将很难调试。

?

为了方便程序员,JavaSE7现在引入了多异常捕获,这样程序员就可以合并多个catch字句为一个单独的代码块,而不需要去使用危险的吞并似的一个异常捕获所有,或者拷贝整个代码块。

例3:

?

public class ExampleExceptionHandlingNew{   public static void main( String[] args )   {         try {                  URL url = new URL("http://www.yoursimpledate.server/");                  BufferedReader reader = new BufferedReader(                           new InputStreamReader(url.openStream()));                  String line = reader.readLine();                  SimpleDateFormat format = new SimpleDateFormat("MM/DD/YY");                  Date date = format.parse(line);         }         catch(ParseException | IOException exception) {                  // handle our problems here.         }   }}
?

?

RethrowingExceptions

public class ExampleExceptionRethrowInvalid{ public static void demoRethrow()throws IOException { try { // forcing an IOException here as an example, // normally some code could trigger this. throw new IOException(“Error”); } catch(Exception exception) { /* * Do some handling and then rethrow. */ throw exception; } } public static void main( String[] args ) { try { demoRethrow(); } catch(IOException exception) { System.err.println(exception.getMessage()); } }}?

public class ExampleExceptionRethrowOld{ public static demoRethrow() { try { throw new IOException("Error"); } catch(IOException exception) { /* * Do some handling and then rethrow. */ throw new RuntimeException(exception); } } public static void main( String[] args ) { try { demoRethrow(); } catch(RuntimeException exception) { System.err.println(exception.getCause().getMessage()); } }}?

public class ExampleExceptionRethrowSE7{ public static demoRethrow() throws IOException { try { throw new IOException("Error"); } catch(Exception exception) { /* * Do some handling and then rethrow. */ throw exception; } } public static void main( String[] args ) { try { demoRethrow(); } catch(IOException exception) { System.err.println(exception.getMessage()); } }}?

Try-with-Resources

public class ExampleTryResources{ public static void main(String[] args) { BufferedReader reader = null; try { URL url = new URL("http://www.yoursimpledate.server/"); reader = new BufferedReader(new InputStreamReader(url.openStream())); String line = reader.readLine(); SimpleDateFormat format = new SimpleDateFormat("MM/DD/YY"); Date date = format.parse(line); } catch (MalformedURLException exception) { // handle passing in the wrong type of URL. } catch (IOException exception) { // handle I/O problems. } catch (ParseException exception) { // handle date parse problems. } finally { if (reader != null) { try { reader.close(); } catch (IOException ex) { ex.printStackTrace(); } } } }}?

public static void main(String[] args){ try (BufferedReader reader = new BufferedReader( new InputStreamReader( new URL("http://www.yoursimpledate.server/").openStream()))) { String line = reader.readLine(); SimpleDateFormat format = new SimpleDateFormat("MM/DD/YY"); Date date = format.parse(line); } catch (ParseException | IOException exception) { // handle I/O problems. }}?

总结:

?

原文连接http://www.oracle.com/technetwork/articles/java/java7exceptions-486908.html

?

,不过说实话写起来更别扭了。 3 楼 javabkb 2012-03-02   赞楼主,讲得很好 4 楼 sangshuye 2012-03-02   论坛应该改下了。。。。。顶的可以不说理由,,,踩的一定说理由。。。。 5 楼 angel243fly 2012-03-02   对翻译工作的辛苦深有体会

热点排行