Swift 2.1 do-try-catch没有捕获错误 [英] Swift 2.1 do-try-catch not catching error
问题描述
这是我的Swift 2.1代码段.出现错误的地方会在错误出现的位置的注释中显示.
Here's my Swift 2.1 code snippet. The error that's occurring is shown in the comments at the point where the error appears.
错误显示在调试面板中,并且应用程序崩溃.该应用程序永远不会在捕获中打印行,也不会按预期方式正常返回.
The error shows up in the debugging panel, and the app crashes. The app never prints the line in the catch, nor does it gracefully return as expected.
let audioFileURL = receivedAudio.filePathURL
guard let audioFile = try? AVAudioFile(forReading: audioFileURL) else {
print("file setup failed")
return
}
let audioFileFrameCount = AVAudioFrameCount(audioFile.length)
audioFileBuffer = AVAudioPCMBuffer(PCMFormat: audioFile.fileFormat, frameCapacity: audioFileFrameCount)
do {
// ERROR: AVAudioFile.mm:263: -[AVAudioFile readIntoBuffer:frameCount:error:]: error -50
// Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'error -50'
// -50 = Core Audio: bad param
try audioFile.readIntoBuffer(audioFileBuffer)
}
catch {
print("unable to load sound file into buffer")
return
}
从我所看到的一切来看,我的do/try/catch格式应该正确.
From everything I've seen, my do/try/catch format should be correct.
audioFile.readIntoBuffer
返回void
,关键字为throws
.
但是,捕获永远不会执行.
Yet, the catch is never executed.
我想念什么?
UPDATE: From Apple's documentation on AVAudioFile
针对:
func readIntoBuffer(_ buffer: AVAudioPCMBuffer) throws
在讨论中:
处理SWIFT错误:
HANDLING ERRORS IN SWIFT:
在Swift中,此API作为初始化程序导入,并用throws关键字标记,以指示在失败的情况下抛出错误.
In Swift, this API is imported as an initializer and is marked with the throws keyword to indicate that it throws an error in cases of failure.
您可以在try表达式中调用此方法,并处理do语句的catch子句中的所有错误,如Swift编程语言(Swift 2.1)中的错误处理和结合使用Cocoa和Objective-C的Swift中的错误处理中所述(快速2.1).
You call this method in a try expression and handle any errors in the catch clauses of a do statement, as described in Error Handling in The Swift Programming Language (Swift 2.1) and Error Handling in Using Swift with Cocoa and Objective-C (Swift 2.1).
From The Swift Programming Language (Swift 2.1): Error Handline
注意
Swift中的错误处理与其他语言中的异常处理类似,使用了try,catch和throw关键字.与许多语言(包括Objective-C)中的异常处理不同,Swift中的错误处理不涉及展开调用堆栈,该过程在计算上可能会非常昂贵.因此,throw语句的性能特征可以与return语句的表现特征相媲美.
Error handling in Swift resembles exception handling in other languages, with the use of the try, catch and throw keywords. Unlike exception handling in many languages—including Objective-C—error handling in Swift does not involve unwinding the call stack, a process that can be computationally expensive. As such, the performance characteristics of a throw statement are comparable to those of a return statement.
最后,来自同一文档:
使用Do-Catch处理错误
Handling Errors Using Do-Catch
您可以使用do-catch语句通过运行代码块来处理错误.如果do子句中的代码引发了错误,则将其与catch子句进行匹配,以确定其中哪一个可以处理该错误.
You use a do-catch statement to handle errors by running a block of code. If an error is thrown by the code in the do clause, it is matched against the catch clauses to determine which one of them can handle the error.
我不必编写并抛出自己的错误/异常即可被抓住.我也应该能够捕获Swift的异常.
推荐答案
do - catch
组合很好.这个问题只是一个无法解决的问题,因此永远不会出现在catch
块中.
The do - catch
combination is fine. This issue is simply one that cannot be caught - and therefore never makes it to the catch
block.
如果问题是可捕获的(通过Swift的throws
功能定义和处理),则将执行catch
块.
If the issue were catchable (defined and handled via Swift's throws
functionality), the catch
block would've been executed.
一些语义:关于术语 error 和 exception 的区别存在一个长期存在的争论.
Some semantics: there is a long-standing argument about the differences between the terms error and exception.
一些开发人员认为这两个术语是不同的.在这种情况下,术语 error 表示旨在解决的问题. Swift的throws
动作适合此处.在这种情况下,组合使用do - catch
可以解决问题.
Some developers consider the two terms to be distinct. In this case, the term error represents an issue that was designed to be handled. Swift's throws
action would fit here. In this case, a do - catch
combination would allow the issue to be caught.
对于这些开发人员,例外代表了无法捕获和处理的意外,通常是致命的问题. (通常,即使您可以捕获它,也将无法处理它.)
An exception, for these developers, represents an unexpected, usually fatal, issue that cannot be caught and handled. (Generally, even if you could catch it, you would not be able to handle it.)
其他人认为这两个术语是等效的和可互换的,无论是否可以解决所讨论的问题. (Apple的文档似乎遵循这种哲学.)
Others consider the two terms to be equivalent and interchangeable, regardless of whether the issue in question can be caught or not. (Apple's documentation seems to follow this philosophy.)
(已更新,重点是答案,而不是语义.)
(Updated to focus on the answer rather than the semantics.)
这篇关于Swift 2.1 do-try-catch没有捕获错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!