V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
whx20202
V2EX  ›  Python

话说 try catch 执行效率低吗?不限于 Python

  •  
  •   whx20202 · Jan 19, 2017 · 6829 views
    This topic created in 3396 days ago, the information mentioned may be changed or developed.

    突然有个想法,如果有下面代码:

    try:
        容易出错的函数()
        后续()                 #这个函数必须等前面函数,不出错才能执行
    catch Exception e:
        pass
    

    你们会把后续函数放在哪里? try-catch 里面还是外面?

    Supplement 1  ·  Jan 19, 2017
    感谢各位,明白了
    21 replies    2017-01-20 10:18:20 +08:00
    Monstercat
        1
    Monstercat  
       Jan 19, 2017
    当然是里面了 try 的意义不就是这样吗
    clino
        2
    clino  
       Jan 19, 2017
    如果后续需要依赖会出错的函数,那应该在里面
    如果后续不依赖,出错了也要执行就放外面
    mgna17
        3
    mgna17  
       Jan 19, 2017
    python 的话,我喜欢放 else 里面
    coolair
        4
    coolair  
       Jan 19, 2017 via Android
    红帽有偏文章,说是像 if xxx==True 这样的语句可以用 try catch 来写尽量用 try catch ,说是更 pythonic
    pppy
        5
    pppy  
       Jan 19, 2017
    尽量用 try except 。(针对 Python
    ●Throwing exceptions is not “ expensive ” in Python unlike e.g. Java.
    ● Rely on duck typing rather than checking for a specific type.
    QAPTEAWH
        6
    QAPTEAWH  
       Jan 19, 2017
    throw/catch (except/catch) 更能表达这个操作的本质, try 只是个定界符。
    DingSoung
        7
    DingSoung  
       Jan 19, 2017
    try 不冷处理所有的异常,不代表 try 了这一块就不会崩溃。
    都知道容易出错了,就把容易出错的说一下处理,类型检查,越界,线程安全等等
    DingSoung
        8
    DingSoung  
       Jan 19, 2017
    看错了 我说的是 Objc
    maomaomao001
        10
    maomaomao001  
       Jan 19, 2017 via Android
    JavaScript 里, tryc 似乎比 if 快很多
    knightdf
        11
    knightdf  
       Jan 19, 2017
    这个。。。刚需而且没有对比,低不低没有意义
    xjp
        12
    xjp  
       Jan 19, 2017 via iPhone
    js 里尽量少用 try catch 因为 v8 引擎不会对 trycatch 进行优化
    otakustay
        13
    otakustay  
       Jan 19, 2017
    1. 大部分情况下,不 throw 的 try 块基本不会有什么损失
    2. 如 @xjp 所说 V8 有这问题,这属于 V8 太烂- -
    3. try/catch 是业务控制,大部分时候容不得你用还是不用,我不建议为了所谓性能去把应该用异常的场合变成 return code 之类的方式
    4. 但这并不代表鼓励你大块大块使用 try/catch ,需要的场合应该都只是一小块的关键代码
    5. 对于 V8 这种场景,如果想减小 try/catch 对优化的影响,把 try/catch 放进一个独立函数中即可
    glasslion
        14
    glasslion  
       Jan 19, 2017
    1. Python 的 try catch 性能损失很小
    2. Python 社区一般会推荐使用 try-catch 风格的异常检查, 因为
    2.1 try-catch 的代码往往比 检查 return code 更精简
    2.2 检查 return code 可能会造成 race condition.
    3. 你样例里的 后续() 不应该放在 try-catch 里。 一般 try-catch 里的代码应该尽量少, 但这和性能无关。
    因为你的本意是捕捉 容易出错的函数() 里的异常, 而这段代码实际上做的是 捕捉 容易出错的函数() 和 后续() 的异常, 而后续() 往往不能直接套用前面的异常处理逻辑
    gamexg
        15
    gamexg  
       Jan 19, 2017 via Android
    @glasslion +1 不喜欢太长。
    前面的异常捕获后直接 return 或继续抛异常。
    ipwx
        16
    ipwx  
       Jan 19, 2017
    try: ... catch ... else: ...
    ryd994
        17
    ryd994  
       Jan 19, 2017 via Android
    @est 你这样欺负解释器语言………
    谁知道解释器自己用掉多少嘛………
    Allianzcortex
        18
    Allianzcortex  
       Jan 19, 2017 via iPhone
    .... . easier ask forgiveness than get permission ,所以写就好了
    eyp82
        19
    eyp82  
       Jan 20, 2017
    Python 的 try..catch 有个蛋疼的问题是, 搞一下这个, 就要往右缩进 4 个字符, 有时逻辑复杂已经缩进了好多实在是不想因为这个再缩进了.
    另外 try catch 包围起来的代码块里代码尽可能少, 尽量只放你要保护的代码, 其他的无关代码全拿到外面.
    wizardoz
        20
    wizardoz  
       Jan 20, 2017
    @eyp82 你说的这个是 try catch 的锅吗? python 中 if else 也有这个头疼的问题
    eyp82
        21
    eyp82  
       Jan 20, 2017
    @wizardoz 你说是缩进吗? 这也不是锅啦. 我有强迫症, 看着代码格式不好看就想给它弄漂亮一点.
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   968 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 192ms · UTC 20:17 · PVG 04:17 · LAX 13:17 · JFK 16:17
    ♥ Do have faith in what you're doing.