2016-11-09 22 views
0

我無法立即捕捉到RuntimeException的StaleStateException,爲什麼?爲什麼設計這個?我無法立即捕捉到StaleStateException,爲什麼?

如果第一個事務沒有提交,我不會趕上例外,但StaleStateException延伸的RuntimeException,疑問,它應該應該抓住它立即

public ResponseInfo catchExce() { 

    try { 
     throwExceServiceA.throwExce(); 
    } catch (StaleStateException e) { 
     System.out.println("This is a StaleStateException"); 
    } catch (Exception e) { 
     System.out.println("This is a StaleStateException. Also is catched here"); 
    } finally { 
     System.out.println("This block is always executed"); 
    } 

} 

ThrowExceServiceA

@Transactional(propagation = Propagation.REQUIRES_NEW) 
public void throwExce() throws InterruptedException { 

    throwExceServiceB.throwExce(); 
    Thread.sleep(1000); 
} 

ThrowExceServiceB

@Transactional(propagation = Propagation.REQUIRES_NEW) 
public void throwExce() { 
    throw new StaleStateException(""); 
} 
+0

你沒有你的代碼的第一個塊的Catch中的代碼,你應該能夠捕獲異常 – cralfaro

+0

我不知道爲什麼你重新發布你的第一個問題。您編輯了其他問題,因此請等到重新打開或刪除它。 – Tom

+1

@Tom我會刪除它 –

回答

0

將此代碼添加到您的代碼:

public ResponseInfo catchExce() { 

    try { 
     throwExceServiceA.throwExce(); 
    } 
    catch (StaleStateException e) { 
     System.out.println("This is a StaleStateException"); 
    } 
    catch (Exception e) { 
     System.out.println("This is a StaleStateException. Also is catched here"); 
    } 
    finally{ 
     System.out.println("This block is always executed"); 
    } 
} 

我希望這個澄清一點您的演示