ios - Wrong format error can't be handled with Try,Catch -
i trying format timestamp, when in other format error calendar.datecomponents. trying throw error if timestamp different. error unexpectedly found nil while unwrapping optional value
should throw error.
do{ compmessage = try calendar.datecomponents([.hour, .minute], from: outputstringtimestamp!) let minute = (string(format: "%02d", (compmessage?.minute!)!)) let hour = (string(format: "%02d", (compmessage?.hour!)!)) return "\(hour):\(minute)" }catch{ return "0" }
how throw error if try not working?
you cannot catch errors thrown force unwrapping of optional values or force casting. if force unwrap optional or force cast value, explicitly tell compiler casting/unwrapping should succeed , can done unsafely, if doesn't succeed, runtime error, programming error, since used force casting/unwrapping.
use safe optional unwrapping if let
or guard let
safely unwrap optionals.
do-catch blocks can used catch errors thrown functions explicitly marked throw
.
Comments
Post a Comment