java 7中的try catch除了之前談到的新特性外,本文簡單來例子小結下,其實還是有不少地方要注意的,首先看一個典型的代碼:
先來兩個異常類:
Java代碼
- public class ExceptionA extends Exception{
- public ExceptionA(String message){
- super(message);
- }
- }
- public class ExceptionB extends Exception{
- public ExceptionB(String message){
- super(message);
- }
- }
public class ExceptionA extends Exception{ public ExceptionA(String message){ super(message); }}public class ExceptionB extends Exception{ public ExceptionB(String message){ super(message); }}
再建立一個資源類oldresource,如下: Java代碼
-
- public class OldResource{
- public void doSomeWork(String work) throws ExceptionA{
- System.out.println("Doing: "+work);
- throw new ExceptionA("Exception occured while doing work");
- }
- public void close() throws ExceptionB{
- System.out.println("Closing the resource");
- throw new ExceptionB("Exception occured while closing");
- }
- }
public class OldResource{ public void doSomeWork(String work) throws ExceptionA{ System.out.println("Doing: "+work); throw new ExceptionA("Exception occured while doing work"); } public void close() throws ExceptionB{ System.out.println("Closing the resource"); throw new ExceptionB("Exception occured while closing"); }}
我們開始使用之: Java代碼
- public class OldTry {
- public static void main(String[] args) {
- OldResource res = null;
- try {
- res = new OldResource();
- res.doSomeWork("Writing an article");
- } catch (Exception e) {
- System.out.println("Exception Message: "+
- e.getMessage()+" Exception Type: "+e.getClass().getName());
- } finally{
- try {
- res.close();
- } catch (Exception e) {
- System.out.println("Exception Message: "+
- e.getMessage()+" Exception Type: "+e.getClass().getName());
- }
- }
- }
- }
public class OldTry { public static void main(String[] args) { OldResource res = null; try { res = new OldResource(); res.doSomeWork("Writing an article"); } catch (Exception e) { System.out.println("Exception Message: "+ e.getMessage()+" Exception Type: "+e.getClass().getName()); } finally{ try { res.close(); } catch (Exception e) { System.out.println("Exception Message: "+ e.getMessage()+" Exception Type: "+e.getClass().getName()); } } }}
看輸出:
Doing: Writing an article
Exception Message: Exception occured while doing work Exception Type: javaapplication4.ExceptionA
Closing the resource
Exception Message: Exception occured while closing Exception Type: javaapplication4.ExceptionB
再來看java 7中的新寫法,代碼如下: Java代碼
- public class NewResource implements AutoCloseable{
- String closingMessage;
-
- public NewResource(String closingMessage) {
- this.closingMessage = closingMessage;
- }
-
- public void doSomeWork(String work) throws ExceptionA{
- System.out.println(work);
- throw new ExceptionA("Exception thrown while doing some work");
- }
- public void close() throws ExceptionB{
- System.out.println(closingMessage);
- throw new ExceptionB("Exception thrown while closing");
- }
-
- public void doSomeWork(NewResource res) throws ExceptionA{
- res.doSomeWork("Wow res getting res to do work");
- }
- }
public class NewResource implements AutoCloseable{ String closingMessage; public NewResource(String closingMessage) { this.closingMessage = closingMessage; } public void doSomeWork(String work) throws ExceptionA{ System.out.println(work); throw new ExceptionA("Exception thrown while doing some work"); } public void close() throws ExceptionB{ System.out.println(closingMessage); throw new ExceptionB("Exception thrown while closing"); } public void doSomeWork(NewResource res) throws ExceptionA{ res.doSomeWork("Wow res getting res to do work"); }}
在JAVA 7中,要自動在try catch中利用到新特性,想不寫那麼多東西就關閉資源,則可以編寫實現 AutoCloseable類的,則都可以利用該特性了。
在主程式中調用:
Java代碼
- public class TryWithRes {
- public static void main(String[] args) {
- try(NewResource res = new NewResource("Res1 closing")){
- res.doSomeWork("Listening to podcast");
- } catch(Exception e){
- System.out.println("Exception: "+
- e.getMessage()+" Thrown by: "+e.getClass().getSimpleName());
- }
- }
- }
public class TryWithRes { public static void main(String[] args) { try(NewResource res = new NewResource("Res1 closing")){ res.doSomeWork("Listening to podcast"); } catch(Exception e){ System.out.println("Exception: "+ e.getMessage()+" Thrown by: "+e.getClass().getSimpleName()); } }}
輸出結果為:
Listening to podcast
Res1 closing
Exception: Exception thrown while doing some work Thrown by: ExceptionA
大家可以思考下為什麼這樣輸出,在新特性中,資源的自動關閉調用了close(),而
NewResource res = new NewResource("Res1 closing")){
已經為closingMessage賦值了,而最後的Exception e是輸出了,suprred掉了
exception a和exception b的輸出。
再看一個多層嵌套的try catch例子
Java代碼
- public class TryWithRes {
- public static void main(String[] args) {
- try(NewResource res = new NewResource("Res1 closing");
- NewResource res2 = new NewResource("Res2 closing")){
- try(NewResource nestedRes = new NewResource("Nestedres closing")){
- nestedRes.doSomeWork(res2);
- }
- } catch(Exception e){
- System.out.println("Exception: "+
- e.getMessage()+" Thrown by: "+e.getClass().getSimpleName());
- }
-
- }
- }
public class TryWithRes { public static void main(String[] args) { try(NewResource res = new NewResource("Res1 closing"); NewResource res2 = new NewResource("Res2 closing")){ try(NewResource nestedRes = new NewResource("Nestedres closing")){ nestedRes.doSomeWork(res2); } } catch(Exception e){ System.out.println("Exception: "+ e.getMessage()+" Thrown by: "+e.getClass().getSimpleName()); } }}
輸出:
Wow res getting res to do work
Nestedres closing
Res2 closing
Res1 closing
Exception: Exception thrown while doing some work Thrown by: ExceptionA
可以看到,後聲明的資源上被最先CLOSE掉的,這裡各自原先的exception都被supressed掉了。還可以用e.getSuppressed() 把屏蔽掉的exception都放來,比如
Java代碼
- public class TryWithRes {
- public static void main(String[] args) {
- try(NewResource res = new NewResource("Res1 closing");
- NewResource res2 = new NewResource("Res2 closing")){
- try(NewResource nestedRes = new NewResource("Nestedres closing")){
- nestedRes.doSomeWork(res2);
- }
- } catch(Exception e){
- System.out.println("Exception: "+
- e.getMessage()+" Thrown by: "+e.getClass().getSimpleName());
- if (e.getSuppressed() != null){
- for (Throwable t : e.getSuppressed()){
- System.out.println(t.getMessage()+
- " Class: "+t.getClass().getSimpleName());
- }
- }
- }
-
- }
- }
public class TryWithRes { public static void main(String[] args) { try(NewResource res = new NewResource("Res1 closing"); NewResource res2 = new NewResource("Res2 closing")){ try(NewResource nestedRes = new NewResource("Nestedres closing")){ nestedRes.doSomeWork(res2); } } catch(Exception e){ System.out.println("Exception: "+ e.getMessage()+" Thrown by: "+e.getClass().getSimpleName()); if (e.getSuppressed() != null){ for (Throwable t : e.getSuppressed()){ System.out.println(t.getMessage()+ " Class: "+t.getClass().getSimpleName()); } } } }}
輸出顯示:
Wow res getting res to do work
Nestedres closing
Res2 closing
Res1 closing
Exception: Exception thrown while doing some work Thrown by: ExceptionA
Exception thrown while closing Class: ExceptionB
Exception thrown while closing Class: ExceptionB
Exception thrown while closing Class: ExceptionB