草庐IT

try-catch-rethrow

全部标签

ios - Firebase 商店 : trying to store to local file gives an error

当我尝试按照文档中的示例从Firebase存储中存储文件时,我收到以下错误:Optional(ErrorDomain=FIRStorageErrorDomainCode=-13000"Anunknownerroroccurred,pleasechecktheserverresponse."UserInfo={object=ProfilePicture/Jf2dFzI7LZNzQkOkdFfa3UWQJyH2.jpg,bucket=mapit-1333.appspot.com,NSLocalizedDescription=Anunknownerroroccurred,pleasechec

swift - 如何在 Swift 中使用 try-catch?

我了解了如何创建自己的错误并使用throws关键字在Swift中触发它们。我不明白的是如何复制在其他语言(或Rubyrescue)中发现的常规try-catch以处理未处理的异常。示例(在Swift中):funcdivideStuff(a:Int,by:Int)->Int{returna/by}letnum=divideStuff(4,by:0)//divideby0exception例如,这是我在C#中处理它的方式:intDivideStuff(inta,intb){intresult;try{result=a/b;}catch{result=0;}returnresult;}如何使

objective-c - Swift Catch 运行时异常

我正在对一个应用程序运行模糊测试,因此我专门寻找未处理的运行时错误。该应用程序是用ObjC和Swift编写的,但单元测试是用Swift编写的。我理解swift的基础不是捕捉任意运行时异常,但这纯粹是为了单元测试。我如何捕获运行时这些异常(即索引越界等) 最佳答案 为了在Swift中捕获Obj-C异常,我使用了一个简单的Obj-C类:#import"ObjC2Swift.h"@implementationObjC+(id)catchException:(id(^)())tryBlockerror:(__autoreleasingNSE

swift - let 在 catch 表达式中的作用是什么?

在《theswiftprogramminglanguage》一书中,他们有如下例子funcmakeASandwich()throws{//...}do{trymakeASandwich()eatASandwich()}catchSandwichError.outOfCleanDishes{washDishes()}catchSandwichError.missingIngredients(letingredients){buyGroceries(ingredients)}我想知道的是这条线catchSandwichError.missingIngredients(letingredie

swift - 无法返回末尾带有 catch block 的 promise 链

这曾经有效,但对于PromiseKit版本6这...funccheckIn(request:CheckinRequest)->Promise{letp=checkinService.checkIn(request:request).then{r->Promiseinreturn.value(r)}.catch{ein}returnp}...给...Cannotconvertreturnexpressionoftype'PMKFinalizer'toreturntype'Promise'如何添加一个catchblock并继续将链返回给调用函数? 最佳答案

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

swift - 错误 : Trying to put the stack in unreadable memory at:

我正在尝试向UIViewController添加其他属性。代码:protocolAdditionalStoredProperties{associatedtypeTitlefuncgetAssociatedObject(key:UnsafePointer,defValue:Title)->Title}extensionAdditionalStoredProperties{funcgetAssociatedObject(key:UnsafePointer,defValue:Title)->Title{guardletactual_value=objc_getAssociatedObjec

java - Netbeans 中的 "Convert to try-with-resources"- Cool Beans?

我在Netbeans7.1.2中有以下代码:BufferedOutputStreambos=newBufferedOutputStream(newFileOutputStream(filename));bos.write(newRawData);bos.close();警告提示我“转换为try-with-resources”。当我选择这样做时,我的代码变为:try(BufferedOutputStreambufferedFos=newBufferedOutputStream(newFileOutputStream(filename))){bufferedFos.write(newRaw

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