草庐IT

try-exception

全部标签

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 - 错误 :could not create the Java Virtual Machine Error:A fatal exception has occured. 程序将退出

我刚刚在我的64位Windows-10操作系统上安装了JavaSEDevelopmentKit8u91。我设置了我的path变量。我在命令提示符中尝试了java--version它给了我一个错误。c:\Users\Onlymanu>java--versionUnrecognizedoption:--versionError:CouldnotcreatetheJavaVirtualMachine.Error:Afatalexceptionhasoccurred.Programwillexit.但是当我尝试java-version时,它起作用了。我尝试初始化_JAVA_OPTIONS环境变

java - 为什么我会得到 "Unhandled exception type IOException"?

我有以下简单的代码:importjava.io.*;classIO{publicstaticvoidmain(String[]args){BufferedReaderstdIn=newBufferedReader(newInputStreamReader(System.in));StringuserInput;while((userInput=stdIn.readLine())!=null){System.out.println(userInput);}}}我收到以下错误消息:----------1.ERRORinio.java(atline10)while((userInput=st

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 - "duck an exception"是什么意思?

在AdvantagesofExceptionsJava™教程的部分:Amethodcanduckanyexceptionsthrownwithinit,therebyallowingamethodfartherupthecallstacktocatchit.[...]...duckinganexceptionrequiressomeeffortonthepartofthemiddlemanmethods.Anycheckedexceptionsthatcanbethrownwithinamethodmustbespecifiedinitsthrowsclause.这里的“鸭子异常”是什

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