You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
OS X El Capitan Version 10.11.5. MacBook Pro. Xcode 8.0 beta (8S128d), Swift 3.0
Additional Detail from JIRA
Votes
0
Component/s
Foundation, LLDB for Swift
Labels
Bug, RunTimeCrash, Runtime
Assignee
None
Priority
Medium
md5: 0eab5249a98d19b74d568f408fcff47a
Issue Description:
I ran across an error that I thought might be caught in a do catch statement using the try keyword but the error was never caught and instead threw a fatal runtime error that crashed my program. The error came from using the jsonObject(with:options:) method on the JSONSerialization object when no valid NSData was set to a variable. I was expecting that when NSData was not correctly set and the method call failed that the method would throw -> AnyObject to be caught in the do catch statement like the documentation says, but instead my do catch statement did not catch the thrown error and it crashed my program.
My issue can obviously be solved by ensuring that there is valid NSData/data before using it in the method call, but my real bug to report here is the fact that I was under the impression that based upon the documentation that the jsonObject(with:options:) method call would throw a catchable AnyObject error.
Here is a code snippet illustration the bug:
funcgetWeatherInfo(fromCurrentURLURLString: String){
// URLString = "https://www.badurl.url"guardletlocationURL = NSURL(string: currentURL) else {
print("Unable to form location URL")
return
}
// To ensure weather data is set correctly use an if let statement to // validate that NSData is set before using it//if let weatherData = NSData(contentsOf: locationURL as URL) {do {
// Here we are optimistically assuming that weather // data is present and assigning it to the variableletweatherData = NSData(contentsOf: locationURLasURL)
// Because weatherData is optimistically set above and we // guarantee that is is present in the jsonObject// call using the as! Data statement then the // try statement throws a fatal runtime error and it is// not caught in the do catch block. Why is it not// caught in the do catch block?letjson = tryJSONSerialization.jsonObject(with: weatherDataas! Data, options: JSONSerialization.ReadingOptions.mutableContainers)
ifletcurrentDict = jsonas? NSDictionary {
ifletcurrentArray = currentDict["currently"] as? NSDictionary {
// Data is parsed here
}else {
print("Unable to deserialze")
}
}
}catchleterrorasNSError {
print("NSError CAUGHT ERROR \(error) -- Never executed")
} catch {
print("Catch Any Other Errors -- Never executed")
}
//}
}
The text was updated successfully, but these errors were encountered:
You're not even getting to the JSON deserialization. Your NSData instance is nil, and the as! cast is unwrapping it without checking. That's a programmer error rather than a recoverable run-time error.
Attachment: Download
Environment
OS X El Capitan Version 10.11.5. MacBook Pro. Xcode 8.0 beta (8S128d), Swift 3.0
Additional Detail from JIRA
md5: 0eab5249a98d19b74d568f408fcff47a
Issue Description:
I ran across an error that I thought might be caught in a do catch statement using the try keyword but the error was never caught and instead threw a fatal runtime error that crashed my program. The error came from using the jsonObject(with:options:) method on the JSONSerialization object when no valid NSData was set to a variable. I was expecting that when NSData was not correctly set and the method call failed that the method would throw -> AnyObject to be caught in the do catch statement like the documentation says, but instead my do catch statement did not catch the thrown error and it crashed my program.
Here is the documentation I am referring to: https://developer.apple.com/reference/foundation/nsjsonserialization/1415493-jsonobject
My issue can obviously be solved by ensuring that there is valid NSData/data before using it in the method call, but my real bug to report here is the fact that I was under the impression that based upon the documentation that the jsonObject(with:options:) method call would throw a catchable AnyObject error.
Here is a code snippet illustration the bug:
The text was updated successfully, but these errors were encountered: