2012-01-18 17 views
11

Gerçekten de istisnai bir durumla karşılaşmam gereken yerle karşılaşıncaya kadar her şey harika. Benbahar jdbcTemplate istisna nasıl yakalanır?

try{} 

blokta

jdbcTemplate.query(something...) 

yerleştirdiğinizde alıyorum:

Unreachable catch block for SQLException. This exception is never thrown from the try statement body. 

Çünkü bu durumda ne yapacağız?

try{ 
    personIdReturnedByDb = jdbcTemplate.queryForInt(sql, p.getEmail(), 
      p.getName(), p.getSurname(), encPw, dateSql); 
} 

catch(SQLException sa){ 


} 

SQLException, bir kontrol istisna, JdbcTemplate.query(...) yöntemlerden herhangi tarafından atılan olmadığı için değil
+2

Herhangi bir SQLExceptions yoktur; Bahar bunları bir RuntimeException içinde sarar; Kök sınıf adını unuttum. –

+0

bunu bir cevap vermelisiniz. – hvgotcodes

cevap

27

(javadoc link). Bahar daha genel bir aile olan DataAccessException birine bu çevirir, Teşekkür soyut uzakta herhangi bir spesifik temel veritabanı uygulaması için çalışma zamanı istisnaları.

4

Sen JdbcTemplate durum yakalamak gerekir

yani

try 
{ 
    // Your Code 
} 
catch (InvalidResultSetAccessException e) 
{ 
    throw new RuntimeException(e); 
} 
catch (DataAccessException e) 
{ 
    throw new RuntimeException(e); 
} 
İlgili konular