ADM-201 dump PMP dumps pdf SSCP exam materials CBAP exam sample questions

17/44 错误处理 – 译学馆
未登录,请登录后再发表信息
最新评论 (0)
播放视频

17/44 错误处理

Error Handling | Python for Beginners [17 of 44]

>> Now, if you’re anything like me and I know I am,
如果你和我一样 是这样的情况
the first time that I get into write some code,
当我第一次写代码的时候
sometimes things go wrong.
有时会有一些问题
Now, maybe whatever it is that you’re doing works flawlessly,
或许无论是什么 你写的代码都能完美运行
but in the real world,
但在现实世界里
things will go sideways,
往往会出一些岔子
and sometimes it will be because of mistakes that you made,
有时候是因为你所犯的错
things that you have control over,
你可以控制的事情
and sometimes it will be because something has changed
有时候则是因为某些东西变动了
like database has gone down,
比如说数据库崩盘
a server name has been changed etc.,
服务器名字变更等等
where now my application isn’t
而我的应用程序并不能
going to be able to accommodate that on the fly,
在匆忙之中就适应这一点
and needs to, well, potentially crash.
因此可能会崩溃
So let’s talk about how we can
那就来说说我们应该如何
deal with those different types of errors.
来处理那些不同类型的错误
But before we talk about how to deal with them,
但在开始讲处理方法之前
we should probably start defining a couple of different terms.
我们可能要先定义一些不同的术语
I want to try and make a very clear distinction
我想尝试非常明确地把
between error handling and debugging
错误处理和调试区分开来
because these are two very different things.
因为这是两个差别很大的东西
Sometimes people will use them to mean something synonymous,
人们有时候会把它们用作同义词
and they’re really not.
但它们真的不是
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.
在推行代码时可以预料到的问题
The most common example of this would be a permissions issue,
最常见的例子包括权限许可问题
a database changing, a server being down, etc.
数据库变动 服务器关闭等等
Those things that happen in the wild,
那些意外发生的事情
those things that happen in the real world,
在现实中发生的事情
there’s things that I do not have control over.
有些事情是我控制不了的
Contrast that with debugging.
对比一下调试
Debugging is when I know that there’s a problem with my code.
调试是我知道我的代码有问题
That it’s potentially giving me a wrong answer,
这可能会得出一个错误的结果
it’s potentially crashing,
可能导致崩溃
and I know that there’s something that I’ve done incorrectly
我还知道是我某些地方做错了
that’s causing my code to go sideways.
才导致我的代码出岔子
That’s debugging.
那是调试
So when we get in and we take a look at things
因此当我们进入代码 查看
like try/except/finally
像try/except/finally
which we’ll talk about in a minute,
这些稍后会讲到的语句
those are not useful tools for handling debugging.
它们都不是用于调试的有用工具
Debugging again, I’ve got a problem in my code,
再重复一次 调试是我的代码有问题
I’m trying to fix that problem,
并且我在尝试解决这个问题
that try/except/finally is where there’s something that’s
try/except/finally语句则是
happened external to my application
我的应用程序外部发生的事情所在
that I couldn’t predict that something might go sideways,
这些事情是我不能预料到会出岔子的
and I want to be able to exit gracefully.
我还希望能够优雅地退出程序
So we want to make sure that there’s
所以我们要确保
a separation between those two.
这两个东西被区别开来
Now when we’re talking about errors,
当我们说到错误
things that can go wrong inside of our code,
也就是代码里会出错的东西的时候
these fall under three different categories,
可以将其分为三类
syntax errors, runtime errors, and logic errors.
语法错误 运行时错误和逻辑错误
Let’s start from the top, a syntax error.
先从头开始 说下语法错误
With a syntax error,
出现语法错误时
a code is not going to run at all.
代码根本不会运行
Believe it or not, you have to choose between the errors.
信不信由你 如果你必须在错误之间进行选择
This is the type of error that you want.
这是你想要的错误类型
This is typically going to be the easiest to try and track down
通常这是最容易尝试追踪的错误
because of the fact that again, your code is just going to fail right then and there,
因为你的代码肯定会立即出故障
and the error message that you’re going to get
你将会得到的报错信息
will typically point you right to where the problem is.
通常也会直接指出问题所在
So if we take a look at our output,
如果我们看下输出的东西
you’ll notice that it’s actually telling me right here,
就会发现这实际上告诉了我问题所在
let me go ahead and circle that.
让我来把它圈出来
It’s telling me right there the line of code.
它在告诉我 那行代码有问题
So if we take a look at our little block of code,
如果我们看一下这一小段代码
and we’ll talk a bit more about if statements later on,
我们稍后也会进一步讨论if语句
what we’re actually missing right out here
实际上 这里的“y”后面
after that “y” is a colon.
缺了一个冒号
So that’s why it’s giving us a syntax error
这就是出现语法错误的原因
because we’re missing a key there.
因为我们在那里缺少了一个键
Now, one really nice thing about Python
Python的一个好处是
is because of the fact that it’s not using curly braces,
因为它不使用大括号
you won’t have to worry about tracking down a curly brace
所以你不用担心在代码里找错误的时候
when trying to figure out what’s wrong with your code.
要去追踪一个大括号
If you’ve done something like Java, or JavaScript,
如果你用的是Java或者是JavaScript
you’ll know that can sometimes cause some problems.
你就会知道 找大括号有时候会挺麻烦的
So syntax errors are good errors. We want those.
所以语法错误是好处理的错误 是我们想要的
Now, runtime errors are the second best type of error.
运行时错误是第二好处理的错误类型
Where a code is running,
在代码运行的地方
something has gone wrong,
出了点问题
and it’s going to blow up.
而且就要爆炸了
Now, in my case here,
在这个例子里
the problem that I’m going to run into is that
将会碰到的问题是
I’m trying to do that classic divide by zero.
我试着做除以0这个经典的运算
When we hit that line in our code,
当我们运行这行代码时
it’s going to give me that error message that you see
你就会看到报错信息
right down there at the bottom, “division by zero”.
在底部出现 “除以0”
It’s also very handily going to point me at
它也很便利地指出了
the line number where the problem occurred.
问题所在的代码行号
Runtime errors are actually pretty decent errors,
运行时错误实际上是相当好处理的错误
because they will give me a little bit of information right upfront
因为它们会给出一些预先提供的信息
to let me know where to start for trying to debug my code.
好让我知道从哪里开始尝试调试代码
Now, when you get a runtime error,
当你遇到一个运行时错误
the basic strategy here is to
基本的应对策略就是
start from the line that it’s given you,
从它告诉你的那一行开始入手
and then work your way up to see where the error occurred.
逐步检查是哪里出的错
Now, one important tip that I want to give you here.
在这里我想给你一个重要提示
When you’re dealing with a runtime error,
当你在处理一个运行时错误的时候
I’m going to guarantee you
我向你保证
the problem is somewhere inside of your code.
问题出在代码内部某处
One of the most common mistakes that I see new developers make
我所见到的新手开发者最常犯的错误之一就是
is they’ll go in, they’ll try something, it’ll go wrong,
他们进入代码 做些尝试 然后出现错误
and they’ll make the assumption that there’s
他们会设想
a problem inside of the framework that they’re using,
是他们正在使用的框架内部有问题
inside of the runtime that they’re using etc.
是正在使用的运行时内部有问题 诸如此类
While it’s technically a possibility,
然而 虽然从技术上来说是有这种可能
chances are it’s not going to be there.
但更多时候问题并不出在那里
So much so that you probably have better luck of hitting
罕见到 比起在框架里找到一个错误
the lottery than you do in finding an error inside of a framework.
中奖可能还更容易一点
Again, I don’t want to say that this doesn’t happen,
再说明一下 我不是说这绝对不会发生
but it’s extremely rare.
但发生的几率实在太小了
I can pretty much guarantee you
我可以在很大程度上保证
that if you’re getting a runtime error,
如果你遇到运行时错误
assuming that it’s not something like a server being down,
假设不是服务器关闭之类的事情
it’s an error inside of the code that you’ve written.
那就是你写的代码内部的错误
Start there, finish there,
从那开始 从那结束
that’s where the problem’s going to be.
那就是问题所在
Let’s close out our conversation about try/except/finally
让我们以一些零星的小知识点结束关于
with a couple of last little odds and ends.
try/except/finally语句的讨论吧
First up you’ll have noticed inside my demo
首先 你会注意到我的演示里
that I had a try except else, not also works.
有try-except-else语句 但也不能运行
Where in that case the else is going to be like
在这个例子里 else语句是
I have up here which is that blank except,
我在上面写的那行空白的except语句
where I’m just not looking for a particular parameter.
在那里我没有追求一个特定的参数
Either one will work just fine.
这两个都可以
For me, I kind of like that except
我个人比较喜欢用这种except语句
just because it’s a little bit more consistent
因为它更能和
with a lot of other programming languages.
很多其他的编程语言协调
But again, feel free to use whatever it is that you might like.
但再强调一次 随便用你喜欢的就好
Now, some final words here.
最后说几句
I know that first bullet point might be a little bit confusing,
我知道第一点可能让你有点疑惑
but hear me out,
但听我的
try/except/finally is not used to find bugs.
try/except/finally语句并不是用来找bug的
Let’s again identify what a bug is.
再重温一次bug的定义
Bug is where I have something wrong in my code,
Bug是我写的代码里出错的地方
where I know that this code will not
我知道 如果遵循这个特定路径
run if it follows this particular path,
或者做这个特定的事情
or does this particular thing,
代码将不会运行
and I have control over them.
并且我可以控制它们
If it’s something where a server might be down
如果是服务器崩了
or I’m getting input from a user,
或者是我从用户处得到输入
where I don’t necessarily always
我不一定总能
have control over those types of things,
控制得了的这类情况
now try/accept/finally is perfect.
此时try/except/finally语句就是最好用的
But if I know there’s a problem in my code,
但如果我知道代码里有问题
that’s not where I’m going to put in that try/except/finally.
那就不该用try/except/finally语句
It’s also worth highlighting the fact that
同样 值得强调的是
you don’t have to catch all errors.
你不需要捕获所有错误
If you’re not going to do anything with it,
如果你不打算做什么
if you’re not going to log it,
不打算记录它
if you’re not going to gracefully exit,
不打算优雅地退出程序
then just leave it alone.
把错误放着不管就好了
I will always remember when I was
我永远记得我那时候
working with a framework that another developer had written,
在用另一个开发者写的程序框架工作
what they had done is they had programmed it such
他们所做的就是编写了这样的程序
that if the database threw an error, that it would catch it,
如果数据库抛出一个错误 程序就会捕获它
and then give me back some just generic error message,
然后给我反馈一些通用的报错信息
which made debugging my application impossible.
这使得我完全不能调试我的应用程序
Because I could never see what the original error message was.
因为我永远看不到原始报错信息是什么
So if you’re not going to do anything with it,
所以如果你不想做任何事情
just let it go.
放任不管就好了
You might be thinking, “Well, wait a minute Christopher,
你可能会想 “等一下 克里斯托弗
that might crash my application.’
这会让我的应用程序崩了的呀”
Well, you know what?
好吧 你知道吗?
Sometimes that’s exactly what we want to have happen.
有时候这正是我们想要的结果
That if our application winds up in
如果应用程序停留在
a state where it’s just flat and not going to work,
一种平稳却不能工作的状态
that’s okay. Let it crash.
那没事 尽管让它崩吧
That’s exactly what crashes are there for.
那正是程序崩的原因
That’s actually sometimes just fine. All right.
事实上有时候崩了也挺好的 那好
The last type of error that we want to highlight is a logic error.
我们要强调的最后一种错误是逻辑错误
Our logic error is when our code compiles properly if you will,
逻辑错误是 如果你愿意 代码可以被正确编译
there’s no syntax errors.
没有语法错误
It doesn’t give us an error message,
没有给出报错信息
so there’s no runtime errors.
所以也没有运行时错误
It just doesn’t give us the response that we’re looking for.
它只是没有给出我们想要的结果
So in my case,
在这个例子里
what you’ll notice is that I’ve
你会注意到
got a couple little variables here.
这里有一些小变量
Let’s go ahead and
继续来看
make sure that I grab my highlighter. There we go.
确保我选中了荧光笔 好了
I’ve got my y being 206,
我设定y=206
my x being 206,
不对 是x=206
I’ve got my y being 42.
y=42
Then what I’m going to do is I’m going to say, “Hey,
接下来我要说 “嘿
if x is greater than y,
如果x大于y
then let’s print out is greater than y.”
那就输出大于y”
Now I go ahead and I run my application,
接下来 运行一下程序
and I don’t get a response.
没有反应
If you were listening carefully,
如果你有认真听讲
you’ll probably have noticed that I actually
你可能已经注意到我实际上
said the incorrect code here.
在这里说了一个错误的代码
That what my code is actually saying is x less than y. Right here.
我的代码实际上写的是“x That’s what I actually wrote inside of my code.
那才是我实际上在代码内部编写的内容
What I had meant to write however is if x is greater than y.
但我想要写的是 如果x大于y
This is without a doubt the most common error,
毫无疑问这是最常见的错误
or most common mistake that I made,
或者是我最常犯的错误
that I will frequently reverse my Boolean.
我会经常反转我的布尔值
Little side note here.
这里提一下
I would definitely recommend taking a look
我十分推荐去了解一下
at unit testing and test-driven development.
单元测试和测试驱动开发
They’re concepts that are beyond the scope of this course,
虽然这两个概念超出了本课程的范围
but what they’re about is writing
但它们是关于如何编写
little automated tests to try and catch mistakes in your code,
很少的自动测试来尝试捕获代码里的错误
and they’re really very much designed
而且它们是专门设计来
to catch these types of mistakes.
捕获这类错误的
I’m a huge fan of unit tests.
我非常喜欢单元测试
Definitely recommend taking a look at unit test inside of Python.
强烈推荐你去了解Python内部的单元测试
In any event, logic errors,
不管怎样 说回逻辑错误
again, are the types of errors where everything runs,
是指那种程序运行
but we just don’t get the right response.
却没有正确响应的情况
So how do we then start tracking all of that down?
那么我们应该如何开始追踪这些错误呢?
Well, if you do wind up getting something going sideways on you,
如果你确实在结束时出了一些岔子
and it potentially throws an error message,
界面可能弹出了一个报错信息
take a look at the stack trace.
那就看一下堆栈跟踪
Stack trace is going to show you
堆栈跟踪将会显示
all of the different calls that have been made,
所有已进行的不同调用
the last calls are at the top,
最后的调用会在栈顶
the most recent ones are down at the bottom.
最近的调用则压入栈底
That’s where your code is going to be.
底部就是你的代码所在的位置
Look for line numbers,
找出行号
that will give you a perfect place to start.
这将会是一个完美的切入点
Now, to try and find your mistake, reread your code,
接着是尝试查找错误 请重读你的代码
check the documentation, as always,
一如既往地检查文档
search the Internet, Stack Overflow is your friend.
上网搜索 Stack Overflow是你的好帮手
Maybe just take a break.
或许休息一下
I can’t tell you the number of times where I’ve
我也数不清我有多少次
been battling a bug in my code,
一直在拼命解决代码里的bug
and I just simply took a walk.
我只是散了散步
Or I went home for the day, had dinner, slept,
又或者是回家过了一整天 吃晚饭 睡觉
woke up the next morning,
第二天早上醒来
and then that’s where my problem is,
终于想到是哪里出了问题
sat down with the computer,
于是坐在电脑前
went in, and fixed it.
进入代码 解决问题
Sometimes you just simply need to walk away.
有时候 你只需要走开一下就好
The other big thing that sometimes you need,
有时候你需要做的另一件大事是
is just another set of eyes.
用另一个视角去看待问题
So if you work with somebody who does Python,
如果你和用Python的人一起工作
have them take a look at your code.
让他们来看看你的代码
Sometimes that fresh viewpoint will be exactly what you
有时候 这种新鲜的观点正是你所需要的
need to try and debug your code.
用来尝试调试代码的东西
That is how we can deal with
这就是我们可以如何处理
the different types of errors inside of Python,
Python里不同种类的错误
and when and how to use that try/except/finally.
还有 何时以及如何使用try/except/finally语句

发表评论

译制信息
视频概述

介绍了Python的三种错误类型以及应该如何进行错误处理,同时介绍了try/except/finally语句的用法

听录译者

收集自网络

翻译译者

Schüwa

审核员

审核员MS

视频来源

https://www.youtube.com/watch?v=HQqqNBZosn8

相关推荐