Comments (8)
@paranoidjk 滥 try 确实非常影响代码质量,在前端建议只在调用第三方接口时使用(比如发 http 请求),这已经是业务层行为,还是比较合理的。
至于框架层,兜住异常并吞掉是自取毁灭的行为。
from blog.
如果每个类中的方法都用try/catch包裹,是否有性能问题?
from blog.
@acthtml 异常可以在最外层统一处理,可以不用每个可能出现异常的地方都写上try/catch
from blog.
@codezyc 我的意思就是使用Decorator之后,对性能影响如何? 不过这个异常捕获方案的确不错。
from blog.
装饰器仅在初始化时工作,此时函数已被替换,性能问题基本上可以忽略。
try catch 造成的性能影响不必担心,try 中直接调用函数,性能几乎不受影响,而带来的可维护性价值很大,见下图:
几乎所有程序都需要错误上报机制,就算自己不写 try catch,库不写 try catch(库中含有大量的 try catch),业务代码最上层也会有 try catch 捕获和收集异常,所以不用纠结,现在就开始用吧~
from blog.
写的太赞了,我也要把这个方案应用到我的项目中
from blog.
用 decorator 来做切面异常处理的思路很赞。
对写 try catch 这一点做下探讨
业务方也不需要判断程序中是否存在异常,而战战兢兢的到处 try catch,因为程序中任何异常都会立刻终止函数的后续执行,不会再引发更恶劣的结果。
在浏览器端可能是这样,但在后端思路可能是 fail fast,不要兜住任何开发者编程时预知不到的错误类型,开发者不应该到处 try catch,由多进程模型来保证高可用,比如 egg 的 cluster。
更严格的语言甚至会要求对 Error 类型做静态的分析和检查,比如 http://www.yinwang.org/blog-cn/2017/05/23/kotlin
from blog.
异常真正的应用场景是异常场景
from blog.
Related Issues (20)
- w3c 规范原味解读 - 3 HTML 语义 结构 接口 HOT 1
- 可视化在线编辑器架构设计
- redux applyMiddleware 原理剖析
- 谈谈 Redux 与 Mobx **的适用场景
- Mobx **的实现原理 HOT 2
- 精读 js 模块化发展 HOT 2
- 精读《单页应用的数据流方案探索》
- 从零开始用 proxy 实现 mobx HOT 1
- 精读 Immutable 结构共享
- Redux 使用可变数据结构
- 面向未来的前端数据流框架 - dob
- dob - 框架实现
- dob - 框架使用 HOT 1
- A proxied version of mobx: dob HOT 1
- 前端数据流哲学 HOT 1
- Mvvm 前端数据流框架精讲 HOT 2
- 六十行代码完成 四则运算 语法解析器
- 一篇看懂 React Hooks HOT 5
- image HOT 22
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from blog.