P17Error handling

`Error handling is when I have a problem with my code that's running, and it's not something that I'm going to be able to predict when I pushed my code out to production.
permissions issue; a database changing; a server being down etc
`Debugging is when I know that there's problem with my code.It's potentially crashing.
Error types : Syntax errors; Runtime errors; Logic errors.
With syntax errors, a code is not going to run at all. This is typically going to be the easiest to try and track down.
Runtime erros,also,they will give you a little bit of imformation right upfront. the basic strategy here is to start from the line that it's given you.
Catching runtime errors,try/except/finally
Logic errors : everything run, but we just don't get the right responde. I frequenly reverse my boolean. Little side note here, i would definitely recommend taking a look at unit testing and test-driven develpment.
Figuring out what went wrong:1. Stack trace: (1)Last calls are on the top;(2) Your code is likely on the bottom; (3)Seek out line numbers. 2.Finding your mistake: (1)Reread your code ;(2) Check the documentation;(3) Search the internet ; (4)Take a break ;(5)Ask others for help.
P18实操
x=42
y=0
print()
try:
print(x/y)
except ZeroDivisionError as e:
print('division by zero')
else:
print('Something else went wrong')
finally:
print('This is our cleanup code')
print()
division by zero
This is our cleanup code
导师语录
try里面就是放 你想执行什么代码;catch就是 运行代码可能会出错 你就把它捕获住;finally里面的代码表示不管有没有错之后都执行;比如说代码出错了 但是你想继续执行 你就可以在except里面做一些操作记录一下 然后继续执行;要是不加的话程序就崩溃了;简单一个来讲就是一个是有备案的 一个是顶风作案 逮住枪毙😂我找对了try里面的错,Except就会执行了
掌握错误处理:代码调试与异常捕获实战
本文讲解了错误处理在软件开发中的重要性,区分了错误类型如语法错误、运行时错误和逻辑错误,并提供了调试和使用try-except-finally语句处理异常的技巧。通过实例演示了如何在代码中捕捉并处理可能出现的问题,以及导师分享的实用建议。
50

被折叠的 条评论
为什么被折叠?



