草庐IT

try-catch-rethrow

全部标签

java - Try/Try-with-resources 和 Connection、Statement 和 ResultSet 关闭

我最近和我的教授讨论了如何处理基本的jdbc连接方案。假设我们要执行两个查询,这就是他提出的publicvoiddoQueries()throwsMyException{Connectioncon=null;try{con=DriverManager.getConnection(dataSource);PreparedStatements1=con.prepareStatement(updateSqlQuery);PreparedStatements2=con.prepareStatement(selectSqlQuery);//SettheparametersofthePrepare

java - Try-with-resources 资源范围

在Java7的try-with-resources构造中,我可以在try语句,超出作用域会自动关闭。但是,我没有发现任何关于可用的资源范围的迹象。具体来说,它是否在声明它的tryblock的catch/finallyblock中可用?我在EclipseKepler中尝试了以下操作,但它给人的印象很复杂:资源变量由ContentAssist(代码完成)提供:QuickFix建议更改为资源变量,但这会递归地产生它试图修复的相同问题:在EclipseBugTracker中提出错误之前,我想知道正确的范围限制是什么。 最佳答案 这种语法称为

Java在try-catch-finally机制中的返回值

我刚遇到下面这段代码:publicclassTestFinally{publicstaticvoidmain(String[]args){intreturnValue=function();System.out.println("Returnvalue:"+returnValue);}publicstaticintfunction(){try{return1;}catch(Exceptione){return2;}finally{return3;}}}毫无疑问,运行此代码将产生“返回值:3”的输出。但是,我很好奇:JVM的内部机制。有谁知道虚拟机是否真的通过覆盖第一个“return1”

java - 在try\finally block中有没有finally不执行的情况?

我正在为面向对象编程的测试学习,我想知道考虑以下代码是否存在任何情况:try{dosomething}catch(someExceptione){}finally{dosomething}finallyblock不会执行? 最佳答案 是的。如果您使JavaVM崩溃或通过native代码搞砸,导致程序终止,或在tryblock内无限循环/等待。只有这三种情况可以避免执行finallyblock。 关于java-在try\finallyblock中有没有finally不执行的情况?,我们在S

java - 永远不会在相应的 try 语句的主体中抛出异常

我在Java中遇到异常处理问题,这是我的代码。当我尝试运行此行时出现编译器错误:thrownewMojException("Blednedane");。错误是:exceptionMojExceptionisneverthrowninbodyofcorrespondingtrystatement代码如下:publicclassTest{publicstaticvoidmain(String[]args)throwsMojException{//TODOAuto-generatedmethodstubfor(inti=1;i这里是MojException的代码:publicclassMoj

java - Maven 给出错误 : try-with-resources is not supported in -source 1. 5

尝试使用IntelliJ12.1.4和Java7使用Maven3.0.5创建jar时出现错误。我能够通过IDE毫无问题地运行该项目,但是当我尝试打包它时我得到以下错误。我的POM的相关部分(取自Sonatype的MavenByExample)是:maven-assembly-pluginjar-with-dependencies错误是:[ERROR]...[33,55]error:diamondoperatorisnotsupportedin-source1.5[ERROR]...[207,7]error:try-with-resourcesisnotsupportedin-sourc

java - 为什么Java的try-with-resource中需要声明

Java7的try-with-resources非常棒,但我无法理解为什么需要在try语句中包含资源声明。我的直觉说以下应该是可能的:CloseableResourcething;try(thing=methodThatCreatesAThingAndDoesSomeSideEffect()){//dosomeinterestingthings}thing.collectSomeStats();唉,这会导致语法错误(神秘地期待;)。将类型定义/声明移动到try语句中是可行的,这当然会将thing移动到相应的范围内。当我想从我的AutoClosable中得到更多而不是关闭时,我可以弄清楚

java - 我什么时候应该在 Java try-catch-finally 中使用 finally-block

我什么时候应该使用代码片段A而不是片段B(即使用片段A有什么好处)?:片段A:try{//codeblockA}catch(Exceptionex){//codeblockB}finally{//codeblockC}片段B:try{//codeblockA}catch(Exceptionex){//codeblockB}//codeblockC 最佳答案 如果您有无论是否抛出异常都必须执行的代码,请使用finallyblock。清理数据库连接等稀缺资源就是一个很好的例子。 关于java

java - 即使在 Junit 测试从 with in 'finally' block 中抛出断言错误后, 'try' block 是否会触发?

finally{}block中的writer.close()方法会在Junit断言错误上运行吗?假设以下代码:@TestpublicvoidtestWriter(){try{writer.open();finalListmyBeans=newArrayList();/**Add2beanstothemyBeansListhere.**/finalintbeansWritten=writer.writeBeans(myBeans);//Saythisassertionerrorbelowistriggeredorg.junit.Assert.assertEquals("Wrongnumb

java - 哪个更好/更有效 : check for bad values or catch Exceptions in Java

在Java中哪个更有效:检查错误值以防止异常或让异常发生并捕获它们?这里有两block示例代码来说明这种差异:voiddoSomething(typevalue1){ResultTyperesult=genericError;if(value1==badvalue||value1==badvalue2||...){result=specificError;}else{DoSomeActionThatFailsIfValue1IsBad(value1);//...result=success;}callback(result);}对比voiddoSomething(typevalue1)