草庐IT

try-catch-else

全部标签

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/finally 的滥用吗?

鉴于多个return语句是可以接受的(我有点不同意,但letusdigress),我正在寻找一种更可接受的方式来实现以下行为:选项A:多次返回,重复代码块publicboolmyMethod(){/*...code...*/if(thisCondition){/*...codethatmustrunatendofmethod...*/returnfalse;}/*...morecode...*/if(thatCondition){/*...theSAMEcodethatmustrunatendofmethod...*/returnfalse;}/*...evenmorecode...*

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 - 用模式替换 if else 语句

我有一个ifelse语句,它可能会在不久的将来增长。publicvoiddecide(StringsomeCondition){if(someCondition.equals("conditionOne")){//someMethod("someParameter");}elseif(someCondition.equals("conditionTwo")){//someMethod("anotherParameter");}..else{someMethod("elseParameter");}}既然这看起来已经很乱了,我认为如果我可以在这里应用任何设计模式会更好。我研究了策略模式,

java - 获取 Apache-Tomcat 的错误 :JRE_HOME variable is not defined correctly when trying to run startup. bat

已结束。此问题需要debuggingdetails.它目前不接受答案。编辑问题以包含desiredbehavior,aspecificproblemorerror,andtheshortestcodenecessarytoreproducetheproblem.这将有助于其他人回答问题。关闭7年前。Improvethisquestion当尝试使用'startup.bat'通过cmd提示符启动Tomcat服务器时,出现错误为-“未正确定义JRE_HOME变量。运行此程序需要环境变量”定义环境路径为-CATALINA_HOME-C:\ProgramFiles\Java\apache-tom

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