Call can throw, but it is not marked with 'try' and the error is not handled报错

所报错警告
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:有异常则程序崩溃(慎用)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容