Giter Club home page Giter Club logo

Comments (8)

ascoders avatar ascoders commented on August 15, 2024 2

@paranoidjk 滥 try 确实非常影响代码质量,在前端建议只在调用第三方接口时使用(比如发 http 请求),这已经是业务层行为,还是比较合理的。

至于框架层,兜住异常并吞掉是自取毁灭的行为。

from blog.

acthtml avatar acthtml commented on August 15, 2024

如果每个类中的方法都用try/catch包裹,是否有性能问题?

from blog.

codezyc avatar codezyc commented on August 15, 2024

@acthtml 异常可以在最外层统一处理,可以不用每个可能出现异常的地方都写上try/catch

from blog.

acthtml avatar acthtml commented on August 15, 2024

@codezyc 我的意思就是使用Decorator之后,对性能影响如何? 不过这个异常捕获方案的确不错。

from blog.

ascoders avatar ascoders commented on August 15, 2024

@acthtml

装饰器仅在初始化时工作,此时函数已被替换,性能问题基本上可以忽略。

try catch 造成的性能影响不必担心,try 中直接调用函数,性能几乎不受影响,而带来的可维护性价值很大,见下图:

image

几乎所有程序都需要错误上报机制,就算自己不写 try catch,库不写 try catch(库中含有大量的 try catch),业务代码最上层也会有 try catch 捕获和收集异常,所以不用纠结,现在就开始用吧~

from blog.

jin5354 avatar jin5354 commented on August 15, 2024

写的太赞了,我也要把这个方案应用到我的项目中

from blog.

paranoidjk avatar paranoidjk commented on August 15, 2024

用 decorator 来做切面异常处理的思路很赞。

对写 try catch 这一点做下探讨

业务方也不需要判断程序中是否存在异常,而战战兢兢的到处 try catch,因为程序中任何异常都会立刻终止函数的后续执行,不会再引发更恶劣的结果。

在浏览器端可能是这样,但在后端思路可能是 fail fast,不要兜住任何开发者编程时预知不到的错误类型,开发者不应该到处 try catch,由多进程模型来保证高可用,比如 egg 的 cluster。
更严格的语言甚至会要求对 Error 类型做静态的分析和检查,比如 http://www.yinwang.org/blog-cn/2017/05/23/kotlin

from blog.

yanlee26 avatar yanlee26 commented on August 15, 2024

异常真正的应用场景是异常场景

from blog.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.