草庐IT

catch-safeguarding

全部标签

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

结合 lambda 和 multi-catch 子句时出现 Java 错误?

importjava.io.*;importjava.net.*;publicclassTest{publicstaticvoidmain(String[]arguments)throwsException{Runnablerunnable=()->{try{throwException();}catch(SocketException|EOFExceptionexception){System.err.println("wrong");}catch(IOExceptionexception){System.err.println("right");}};runnable.run();

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 - 在一个 catch block 中处理多种不同类型的异常?

如果在catch()中允许多个异常,那么它将减少冗余错误处理代码的数量。例如,try{//somestatments}catch(Type1Exceptiont1,Type2Exceptiont2,Type3Exceptiont3){//wishifthiscouldbeallowed/*t1,t2,t3arechildrenofExceptionandneedssameerrorhandlingthenwhytohavedifferentcatchblockswithsamepieceofcode*/} 最佳答案 是的-这就是为什

Java在try-catch-finally机制中的返回值

我刚遇到下面这段代码:publicclassTestFinally{publicstaticvoidmain(String[]args){intreturnValue=function();System.out.println("Returnvalue:"+returnValue);}publicstaticintfunction(){try{return1;}catch(Exceptione){return2;}finally{return3;}}}毫无疑问,运行此代码将产生“返回值:3”的输出。但是,我很好奇:JVM的内部机制。有谁知道虚拟机是否真的通过覆盖第一个“return1”

java - 我什么时候应该在 Java try-catch-finally 中使用 finally-block

我什么时候应该使用代码片段A而不是片段B(即使用片段A有什么好处)?:片段A:try{//codeblockA}catch(Exceptionex){//codeblockB}finally{//codeblockC}片段B:try{//codeblockA}catch(Exceptionex){//codeblockB}//codeblockC 最佳答案 如果您有无论是否抛出异常都必须执行的代码,请使用finallyblock。清理数据库连接等稀缺资源就是一个很好的例子。 关于java

java - 哪个更好/更有效 : check for bad values or catch Exceptions in Java

在Java中哪个更有效:检查错误值以防止异常或让异常发生并捕获它们?这里有两block示例代码来说明这种差异:voiddoSomething(typevalue1){ResultTyperesult=genericError;if(value1==badvalue||value1==badvalue2||...){result=specificError;}else{DoSomeActionThatFailsIfValue1IsBad(value1);//...result=success;}callback(result);}对比voiddoSomething(typevalue1)

java - try-catch 除以零

我的问题是关于简单除以零示例的try-catchblock。你看到第一行了吗?如果我将这两个变量中的任何一个转换为double,程序将无法识别catchblock。在我看来,无论我投还是不投,都必须执行catchblock。这段代码有什么问题?publicstaticvoidmain(String[]args){intpay=8,payda=0;try{doubleresult=pay/(double)payda;//ifIcastanyofthetwovariables,programdoesnotrecognizethecatchblock,whyisitso?System.out

java:如何声明一个在 try - catch block 中初始化的变量的 final?

我有一个变量在初始化后不应更改其值,因此我想将其定义为最终变量。问题是变量必须在tryblock中初始化,所以我遇到了以下麻烦:我有以下代码:Connectionconn=null;try{conn=getConn(prefix);[...dosomestuffwithconn...]}catch(Exceptione){thrownewDbHelperException("erroropeningconnection",e);}finally{closeConnection(conn);}如果我将variabale声明为final,但没有将其初始化为null,我会在finallybl