博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
swift try try? try!
阅读量:6895 次
发布时间:2019-06-27

本文共 1126 字,大约阅读时间需要 3 分钟。

try

You have 2 options when you try calling a function that may throw.

You can take responsibility of handling errors by surrounding your call within a do-catch block:

 

Or just try calling the function, and pass the error along to the next caller in the call chain:

 

try!

What happens when you try to access an implicitly unwrapped optional with a nil inside it? Yes, true, the app will CRASH! Same goes with try! it basically ignores the error chain, and declares a “do or die” situation. If the called function didn’t throw any errors, everything goes fine. But if it failed and threw an error, your application will simply crash.

 

try?

A new keyword that was introduced in Xcode 7 beta 6. It returns an optional that unwraps successful values, and catches error by returning nil.

 

Or we can use new awesome guard keyword:

 

One final note here, by using try? note that you’re discarding the error that took place, as it’s translated to a nil. Use try? when you’re focusing more on successes and failure, not on why things failed.

 

https://stackoverflow.com/questions/32390611/try-try-try-what-s-the-difference-and-when-to-use-each

转载地址:http://jhzdl.baihongyu.com/

你可能感兴趣的文章
Java解析Excel文件
查看>>
MySQL数据类型简介
查看>>
由于未预料的错误,现在无法使用nautilus
查看>>
python很low的三级菜单(六)
查看>>
Go语言之Writer 和 Reader
查看>>
linux 位置参数 特殊变量 read grep 变量赋值
查看>>
spool+sql拼接实现导出结果集为csv格式文件
查看>>
【19】Python工资管理系统
查看>>
HAProxy+Keepalived实现Web服务器负载均衡
查看>>
配置Linux主机SSH无密码访问
查看>>
servlet接收乱码处理方案
查看>>
自动化运维之Ansible的安装与简单入门命令
查看>>
mysql互为主从的环境,更新一条语句同时提交,为什么会出现数据不一致?
查看>>
Vmware软件安装精讲
查看>>
mysql双主模式
查看>>
rpm 安装lamp
查看>>
区块链真的有这么厉害吗?--初识区块链后的感想(一)
查看>>
mongodb Profiling 通过慢查询日志分析查询慢的原因 相应优化
查看>>
Memcached管理与监控工具 MemAdmin
查看>>
mysql 主从复制读写分离
查看>>