草庐IT

swift - 在 Swift 中使用 Do/Catch

我正在开发一个应用程序,想从一个函数中取回数据。但是有时数据丢失或与我想要检索的数据不同。我是Swift的新手,我找不到一种方法来编写一个函数来进行一些处理并返回这些数据。当此数据丢失时,该函数应返回一个字符串“NotFound”。像这样:funcprocessData(data:String){do{//processingvarresult=processedData}catch{varresult="NotFound"}returnresult}如果有人能帮助我,那就太好了。 最佳答案 你应该检查result是否为nil。fu

swift - 如何从 catch 子句中的 NSError 获取 userInfo

如果我有一个throw方法,像这样:funcdoSomethingWithString(string:String)throws{guardstring.characters.count>0else{throwNSError(domain:"CustomErrorDomain",code:42,userInfo:["foo":"bar"])}//Dosomethingwithstring...}然后我尝试调用它并读取userInfo:do{trydoSomethingWithString("")}catchleterrorasNSError{print(error.domain)pri

java - 有一个 try-catch block ,你应该把所有的语句都放在里面还是只放不安全的?

假设save抛出并且i仅用于save。以下代码片段是否相同?请考虑语义、性能和其他方面。voidbob(){inti=calculate();try{save(i);}catch(Exceptione){report(e)}}对比voidbob(){try{inti=calculate();save(i);}catch(Exceptione){report(e)}}一般来说,我想知道,是应该将一个函数的所有语句都放在try-catchblock中,还是只放在一个抛出的语句中。 最佳答案 在语义方面,如果您已经决定要将try-catc

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