草庐IT

catching

全部标签

java - 什么是先到的 - finally 还是 catch block ?

考虑以下测试用例:publicclassMain{staticinta=0;publicstaticvoidmain(String[]args){try{test();System.out.println("---");test2();}catch(Exceptione){System.out.println(a+":outercatch");a++;}}publicstaticvoidtest(){try{thrownewException();}catch(Exceptione){System.out.println(a+":innercatch");a++;}finally{Sy

java - 为什么 try-with-resources catch block 是选择性可选的?

我读到try-with-resources中的catchblock是可选的。我尝试在try-with-resourcesblock中创建一个Connection对象,没有后续的catchblock,只是为了从eclipse中获取编译器错误:“由自动close()调用引发的未处理的异常类型SQLException。”由于可以在try-with-resources中使用的每个资源都实现AutoCloseable,因此在调用close()方法时可能会引发异常,我不'不明白catch子句如何是可选的,因为它不允许我跳过从close()捕获异常。AutoCloseable的具体实现是否有特殊要求

java - 在 Java 中,try、catch 和 finally 中的 return 是如何工作的?

我无法准确理解return在try、catch中的工作原理。如果我有try和finally而没有catch,我可以将return放入tryblock。如果我有try、catch、finally,我不能把return放在尝试block。如果我有一个catchblock,我必须将return放在try、catch之外,finallyblock。如果我删除catchblock和throwException,我可以将return放在tryblock内.它们究竟是如何工作的?为什么我不能将return放在tryblock中?带有try、catch、finally的代码publicintinser

java - 为什么要在没有 Catch 或 finally 的情况下编写 Try-With-Resources?

为什么要像下面的例子那样写TrywithoutaCatch或finally?protectedvoidprocessRequest(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{response.setContentType("text/html;charset=UTF-8");try(PrintWriterout=response.getWriter()){/*TODOoutputyourpagehere.Youmayusefollowingsample

java - Try With Resources vs Try-Catch

这个问题在这里已经有了答案:What'sthepurposeoftry-with-resourcesstatements?(7个回答)关闭2年前。我一直在查看代码,并且已经看到尝试使用资源。我以前使用过标准的try-catch语句,看起来他们做同样的事情。所以我的问题是TryWithResourcesvsTry-Catch它们之间有什么区别,哪个更好。这里是资源的尝试:objectsjar=newobjects("brand");objectscan=newobjects("brand");try(FileOutputStreamoutStream=newFileOutputStrea

java - Try-catch-finally 然后再次 try catch

我经常遇到这样的情况:-try{...stmts...}catch(Exceptionex){...stmts...}finally{connection.close//throwsanexception}finally内部仍然需要一个try-catchblock。克服这个问题的最佳做法是什么? 最佳答案 编写一个SQLUtils类,其中包含捕获和记录此类异常的staticcloseQuietly方法,然后酌情使用。你最终会得到如下内容:publicclassSQLUtils{privatestaticLoglog=LogFacto

Java try/catch 性能,是否建议将 try 子句中的内容保持在最低限度?

考虑到你有这样的代码:doSomething()//thismethodmaythrowacheckedaexception//dosomeassignementscalculationsdoAnotherThing()//thismethodmayalsothrowthesametypeofcheckedexception//morecallstomethodsandcalculations,allthrowingthesamekindofexceptions.现在我知道了,实际上在构造异常时会影响性能,特别是展开堆栈。而且我还阅读了几篇文章,指出在输入try/catchblock时

java - 哪个更快,在 java 中尝试 catch 或 if-else(WRT 性能)

哪个更快:这个try{n.foo();}catch(NullPointerExceptionex){}或if(n!=null)n.foo(); 最佳答案 这不是哪个更快的问题,而是正确性的问题。异常(exception)的情况异常(exception)。如果n可能是null作为正常业务逻辑的一部分,则使用if..else,否则抛出异常。 关于java-哪个更快,在java中尝试catch或if-else(WRT性能),我们在StackOverflow上找到一个类似的问题:

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 - 为什么 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