zoukankan      html  css  js  c++  java
  • Swift处理异常的三种方式-try

     1 //  方式一:try方式 程序员手动捕捉异常
     2         do {
     3                 try NSJSONSerialization.JSONObjectWithData(jsonData, options: .MutableContainers)
     4             } catch {
     5                 // error异常的对象
     6                 print(error)
     7             }
     8 
     9 //  方式二:try?方式(常用方式) 系统帮助我们处理异常,如果该方法出现了异常,则该方法返回nil.如果没有异常,则返回对应的对象
    10     guard let anyObject = try? NSJSONSerialization.JSONObjectWithData(jsonData, options: .MutableContainers) else {
    11         return
    12     }
    13 
    14 //  方式三:try!方法(不建议,非常危险) 直接告诉系统,该方法没有异常.注意:如果该方法出现了异常,那么程序会报错(崩溃)
    15      let anyObject = try!NSJSONSerialization.JSONObjectWithData(jsonData, options: .MutableContainers)
  • 相关阅读:
    维护win10注册表
    win10操作技巧
    无处不网络
    事件驱动编程思想
    流程控制之if...else
    python----GIL的概念
    并发与同步异步的概念
    实现并发join的方法
    线程的调用
    三元运算符
  • 原文地址:https://www.cnblogs.com/panda1024/p/6283100.html
Copyright © 2011-2022 走看看