草庐IT

Try-Catch-Finally

全部标签

java - 为什么我们使用 finally block ?

这个问题在这里已经有了答案:Whyusefinally(9个回答)关闭去年。据我所知,以下两个代码片段的用途相同。为什么会有finallyblock?代码A:try{/*Somecode*/}catch{/*Exceptionhandlingcode*/}finally{/*Cleanupcode*/}代码B:try{/*Somecode*/}catch{/*Exceptionhandlingcode*/}//Cleanupcode 最佳答案 如果抛出您未处理的异常会怎样?(我希望你没有发现Throwable...)如果你从tryb

java - try-finally 和 try-catch 的区别

有什么区别try{fooBar();}finally{barFoo();}和try{fooBar();}catch(Throwablethrowable){barFoo(throwable);//Doessomethingwiththrowable,logsit,orhandlesit.}我更喜欢第二个版本,因为它让我可以访问Throwable。这两种变体之间是否存在逻辑差异或首选约定?另外,有没有办法从finally子句访问异常? 最佳答案 这是两个不同的东西:只有在tryblock中抛出异常时才会执行catchblock。fin

java - try-finally 和 try-catch 的区别

有什么区别try{fooBar();}finally{barFoo();}和try{fooBar();}catch(Throwablethrowable){barFoo(throwable);//Doessomethingwiththrowable,logsit,orhandlesit.}我更喜欢第二个版本,因为它让我可以访问Throwable。这两种变体之间是否存在逻辑差异或首选约定?另外,有没有办法从finally子句访问异常? 最佳答案 这是两个不同的东西:只有在tryblock中抛出异常时才会执行catchblock。fin

java - 在 Java 中是否可以在同一个 catch block 中捕获两个异常?

这个问题在这里已经有了答案:CanIcatchmultipleJavaexceptionsinthesamecatchclause?(10个回答)关闭8年前。我需要捕获两个异常,因为它们需要相同的处理逻辑。我想做类似的事情:catch(Exceptione,ExtendsRuntimeExceptionre){//commonlogictohandlebothexceptions}是否可以避免在每个catchblock中重复处理程序代码? 最佳答案 Java7及更高版本Multiple-exceptioncatches受支持,从Ja

java - 在 Java 中是否可以在同一个 catch block 中捕获两个异常?

这个问题在这里已经有了答案:CanIcatchmultipleJavaexceptionsinthesamecatchclause?(10个回答)关闭8年前。我需要捕获两个异常,因为它们需要相同的处理逻辑。我想做类似的事情:catch(Exceptione,ExtendsRuntimeExceptionre){//commonlogictohandlebothexceptions}是否可以避免在每个catchblock中重复处理程序代码? 最佳答案 Java7及更高版本Multiple-exceptioncatches受支持,从Ja

java - finally block 是否总是运行?

是否有任何情况下finally可能无法在java中运行?谢谢。 最佳答案 来自SunTutorialsNote:IftheJVMexitswhilethetryorcatchcodeisbeingexecuted,thenthefinallyblockmaynotexecute.Likewise,ifthethreadexecutingthetryorcatchcodeisinterruptedorkilled,thefinallyblockmaynotexecuteeventhoughtheapplicationasawholec

java - finally block 是否总是运行?

是否有任何情况下finally可能无法在java中运行?谢谢。 最佳答案 来自SunTutorialsNote:IftheJVMexitswhilethetryorcatchcodeisbeingexecuted,thenthefinallyblockmaynotexecute.Likewise,ifthethreadexecutingthetryorcatchcodeisinterruptedorkilled,thefinallyblockmaynotexecuteeventhoughtheapplicationasawholec

java - 我应该如何在 JDBC 中使用 try-with-resources?

我有一种使用JDBC从数据库中获取用户的方法:publicListgetUser(intuserId){Stringsql="SELECTid,nameFROMusersWHEREid=?";Listusers=newArrayList();try{Connectioncon=DriverManager.getConnection(myConnectionURL);PreparedStatementps=con.prepareStatement(sql);ps.setInt(1,userId);ResultSetrs=ps.executeQuery();while(rs.next())

java - 我应该如何在 JDBC 中使用 try-with-resources?

我有一种使用JDBC从数据库中获取用户的方法:publicListgetUser(intuserId){Stringsql="SELECTid,nameFROMusersWHEREid=?";Listusers=newArrayList();try{Connectioncon=DriverManager.getConnection(myConnectionURL);PreparedStatementps=con.prepareStatement(sql);ps.setInt(1,userId);ResultSetrs=ps.executeQuery();while(rs.next())

java - 为什么在 catch InterruptException block 中调用 Thread.currentThread.interrupt()?

为什么要在catchblock中调用Thread.currentThread.interrupt()方法? 最佳答案 这样做是为了保持状态。当您捕获InterruptedException并将其吞下时,您基本上可以防止任何更高级别的方法/线程组注意到中断。这可能会导致问题。通过调用Thread.currentThread().interrupt(),你设置了线程的中断标志,所以更高级别的中断处理程序会注意到它并可以适本地处理它。JavaConcurrencyinPractice在第7.1.3章:响应中断中更详细地讨论了这一点。它的规