美文网首页
Call can throw, but it is not ma

Call can throw, but it is not ma

作者: gezhenrong | 来源:发表于2019-03-29 16:02 被阅读0次
所报错警告
Call can throw, but it is not marked with 'try' and the error is not handled

解决方法:

        if fileManager.fileExists(atPath: self.creatFilePath(), isDirectory: &directory){
           try? fileManager.createDirectory(atPath: self.creatFilePath(), withIntermediateDirectories: true, attributes: nil)
        }

备注

在调用一个函数时,如果发现该函数最后有一个throws,表示该函数可能会抛出异常,处理方式有三种 try try? try!

  • try 捕捉异常,并且进行处理
  • try? 系统会自动处理异常, 返回一个可选类型(如果有异常则返回一个nil,如果没有异常则返回对应的值)
  • try! 告诉系统该函数不会有异常 直接返回值 ps:有异常则程序崩溃(慎用)

相关文章

网友评论

      本文标题:Call can throw, but it is not ma

      本文链接:https://www.haomeiwen.com/subject/zfolbqtx.html