草庐IT

try-exception

全部标签

Java 7 自动资源管理 JDBC(try-with-resources 语句)

如何将创建/接收连接、查询数据库和可能处理结果的常见JDBC习惯用法与Java7的自动资源管理、try-with-resources语句集成?(Tutorial)在Java7之前,通常的模式是这样的:Connectioncon=null;PreparedStatementprep=null;try{con=getConnection();prep=prep.prepareStatement("Update...");...con.commit();}catch(SQLExceptione){con.rollback();throwe;}finally{if(prep!=null)pre

java - throw e 和 throw new Exception(e) 有什么区别?

考虑:try{//Somecodehere}catch(IOExceptione){throwe;}catch(Exceptione){throwe;}throwe和thrownewException(e)有什么区别?try{//Somecodehere}catch(IOExceptione){thrownewIOException(e);}catch(Exceptione){thrownewException(e);} 最佳答案 如果您不需要调整异常类型,则重新抛出(进一步抛出)同一个实例而不做任何更改:catch(IOExcep

java - 中断是否只留下 try/catch 或周围的循环?

如果我在while循环中有一个try...catchblock,并且在catch中有一个break,程序执行是否离开循环?如:while(!finished){try{doStuff();}catch(Exceptione){break;}}doStuff()中抛出的异常会退出循环吗? 最佳答案 是的,它会的。最简单的方法就是尝试一下。publicstaticvoidmain(String[]args){inti=0;while(i它会打印出来01234567outofloop输出以0开头。

java - java try block 的范围是否应该尽可能紧密?

有人告诉我,使用Java的try-catch机制会产生一些开销。因此,虽然有必要将抛出已检查异常的方法放在tryblock中以处理可能的异常,但从性能角度来看,最好将tryblock的大小限制为仅包含可能抛出异常的那些操作。我不太确定这是一个合理的结论。考虑以下两个处理指定文本文件的函数的实现。即使第一个确实会产生一些不必要的开销,我发现它更容易理解。仅通过查看语句就不太清楚异常究竟来自何处,但评论清楚地表明了哪些语句是负责任的。第二个比第一个更长更复杂。特别是,第一个很好的读行习惯必须被破坏以使readLine调用适合tryblock。在定义中可能引发多个异常的函数中处理异常的最佳做

java - 什么时候应该使用 Throwable 而不是 new Exception?

鉴于:Throwable是Exception的父类(superclass)。当我阅读有关编写自己的“异常”的文本时,我看到catchblock中使用了Throwable的示例,其他文本显示newException()在catchblock中使用。我还没有看到关于何时应该使用每个的解释。我的问题是,什么时候应该使用Throwable,什么时候应该使用newException()?在catch或elseblock内使用:throwthrowable;或thrownewException(); 最佳答案 总是抛出Exception(绝不是

java - 使用新的 try-with-resources block 在 SQLException 上回滚事务

我对try-with-resources有疑问,我只是想确定一下。如果我需要对异常使用react,并且我仍然需要catchblock中的资源,我可以使用它吗?给出的例子是这样的:try(java.sql.Connectioncon=createConnection()){con.setAutoCommit(false);Statementstm=con.createStatement();stm.execute(someQuery);//causesSQLException}catch(SQLExceptionex){con.rollback();//dootherstuff}我担心在

java - 为什么 Try/Catch block 会创建新的变量范围?

例如:try{SomeObjectsomeObject=newSomeObject();someObject.dangerousMethod();}catch(Exceptione){}someObject.anotherMethod();//can'taccesssomeObject!但是你可以在try/catchblock之前声明它,然后它就可以正常工作了:SomeObjectsomeObject;try{someObject=newSomeObject();someObject.dangerousMethod();}catch(Exceptione){}someObject.an

Java io丑陋的try-finally block

是否有一种不那么丑陋的方式来处理close()异常以关闭两个流然后:InputStreamin=newFileInputStream(inputFileName);OutputStreamout=newFileOutputStream(outputFileName);try{copy(in,out);}finally{try{in.close();}catch(Exceptione){try{//eventifin.closefails,needtoclosetheoutout.close();}catch(Exceptione2){}throwe;//andthrowthe'in'e

java.lang.Exception : No runnable methods exception in running JUnits

我正在尝试在我的Linux命令提示符下运行JUnit/opt/junit/包含必要的JARS(hamcrest-core-1.3.jar和junit.jar)和类文件,我是使用以下命令运行JUnit:java-cphamcrest-core-1.3.jar:junit.jar:.org.junit.runner.JUnitCoreTestRunnerTestJunit类:importorg.junit.Test;importstaticorg.junit.Assert.assertEquals;publicclassTestJunit{@TestpublicvoidtestAdd(){

java - IntelliJ IDE 在将 Try-Catch 与资源一起使用时出错

我正在尝试使用JDK7的“try-catchwithresources”语句;IntelliJ突出显示我的资源行,说Try-with-resourcesarenotsupportedatthislanguagelevel.当我尝试编译时,我得到:java:try-with-resourcesisnotsupportedin-source1.6(use-source7orhighertoenabletry-with-resources)我检查了我当前的项目是否启用了try-with-resources,并且我的项目正在使用JDK7(库:C:\ProgramFiles\Java\jdk1.