草庐IT

Try-Except-Finally

全部标签

java - 安卓工作室 : Warning while I try to move Class to another package

这是一条错误消息。MethodObject.toString(),referencedinmethodSettingActivity.saveDataButtons(),willnotbeaccessibleinmodulepersonal-health-assistantbackup29octMethodString.trim(),referencedinmethodSettingActivity.setNullCurrentFocusedEditText(),willnotbeaccessibleinmodulepersonal-health-assistantbackup29oc

java - try-catch 和 final 变量

这个问题在这里已经有了答案:Couldafinalvariablebereassignedincatch,evenifassignmentislastoperationintry?(12个答案)variablemightalreadyhavebeenassignedwhenitcannotbeassigned(4个答案)关闭7年前。我有一个非常愚蠢的问题要问你:)例如,我有以下代码片段:classMyClass{publicstaticvoidmain(String[]args){finalStringstatus;try{method1();method2();method3();s

java - 这个 `try..catch..finally` 是多余的吗?

publicFoodoDangerousStuff()throwsException{try{dangerousMethod();returnnewFoo();}catch(Exceptione){throwe;}finally{mustBeCalledAfterDangerousMethod();}}这与我们省略catch子句的行为有什么不同吗?publicFoodoDangerousStuff()throwsException{try{dangerousMethod();returnnewFoo();}finally{mustBeCalledAfterDangerousMethod

java - 在 finally block 中关闭文件不起作用

try{FileReaderfr=newFileReader(file);BufferedReaderbr=newBufferedReader(fr);Stringline=null;}catch(FileNotFoundExceptionfnf){fnf.printStackTrace();}finally{fr.close();}fr.close()显示错误:frcannotberesolved我读到在finallyblock中关闭文件是一个很好的做法。那我做错了什么? 最佳答案 变量fr只在tryblock中有效。它超出了fi

java - 棘手的 try-catch java 代码

publicclassStrange1{publicstaticvoidmain(String[]args){try{Missingm=newMissing();}catch(java.lang.NoClassDefFoundErrorex){System.out.println("Gotit!");}}}publicclassStrange2{publicstaticvoidmain(String[]args){Missingm;try{m=newMissing();}catch(java.lang.NoClassDefFoundErrorex){System.out.println

java - 在 finally block 中将对象引用设置为 null

publicvoidtestFinally(){System.out.println(setOne().toString());}protectedStringBuildersetOne(){StringBuilderbuilder=newStringBuilder();try{builder.append("Cool");returnbuilder.append("Return");}finally{builder=null;/*;)*/}}为什么输出是CoolReturn,而不是null?问候,马亨德拉Athneria 最佳答案

java - 在没有新对象实例的情况下使用 try-with-resources 是错误的形式吗?

一般来说,我总是看到try-with-resources用于分配一个新对象实例,其close()方法被调用为它超出了范围。据我所知,创建一个新对象不是必需的,try-with-resources语法只需要一个局部变量来在超出范围时调用close()。因此,您可以使用它来控制“配对操作”,例如从池中分配一些东西并确保它被返回。例如,下面的MyHandle显示了如何在不再需要时释放池实例://initclassMyHandleimplementsAutoCloseable{booleaninUse=false;publicMyHandleallocate(){inUse=true;retu

java - 在 IntelliJ IDEA 中,如何使用 try-with-resources 包围?

在IntelliJIDEA中,我可以按“Surroundwith”快捷键CTRL-ALT-T用try/catchblock包围一个代码块,等等东西。我想将资源部分包围在一个try-with-resourcesblock中:Writerout=Files.newBufferedWriter(destination,StandardCharsets.UTF_8);temp.process(model,out);对此:try(Writerout=Files.newBufferedWriter(destination,StandardCharsets.UTF_8)){temp.process(

java - 分解出 finally {...} block

我想知道以下两个swnippest在语义上是否相同,如果不相同,有什么区别(我们假设我们要计算类型R的结果,并希望防止可能抛出的异常X这样做的过程):publicRtcf(....){try{Rsome=...;...computetheresult....returnsome;}catch(Xexception){...exceptionhandling....}finally{...cleanup....}}以及以下内容:publicRtc(....){try{Rsome=...;...computetheresult....returnsome;}catch(Xexception

java - 在 finally block 中抛出异常是性能问题吗?

在RationalApplicationDeveloper(基于eclipse的RAD)中,在软件分析器下,我看到代码审查评论(在Performance=>Memory部分下)说“避免在finally中使用throw语句”。在finallyblock中定义throw如何影响性能?这是代码片段,我们已经建议更改代码以记录异常跟踪并且不要抛出异常,}finally{if(bufferedReader!=null){try{bufferedReader.close();}catch(finalIOExceptionex){throwex;}}}我只是想知道这会如何影响内存和性能?