3-1-5-11. Errors and Exceptions

There’s actually a much simpler way of dealing with this in Python.

Here, you learn how to handle errors with try and except blocks.

First, let’s learn more about what errors in Python are.

We’ve seen error messages due to several reasons in this course,

and we can separate them into two types,

syntax errors and exceptions.

Syntax errors occur when Python can’t interpret

our code since we didn’t follow the correct syntax for Python.

These are errors you’re likely to get when you make a typo,

or you’re first starting to learn Python.

Here, I get a syntax error because I

missed the ending quotation mark at the end of the string.

I can see this in the error message,

SyntaxError: End of line when scanning string literal.

The other kind of error,

exceptions occur when unexpected things happen during the execution of the code.

Here, although our code is syntactically correct,

we run into an exception because a string spelling out

10 isn’t a valid argument for the int function.

There are different types of built-in exceptions in Python,

and you can see which exception is being thrown here in the error message.

Value error is one type of exception.

It occurs when a built-in operation or function is

given an argument with the correct type but an inappropriate value.

Here’s another example that there was an exception.

Here, we try to reference a variable name that we didn’t define yet.

So, we get a name error exception.

Again, this code is syntactically correct,

but when Python gets to this line of code,

it can’t find the value for this variable when it tries to run it.

So, it raises an exception and stops the code.

지난 영상에서 보셨듯이

모든 종류의 시나리오를 처리하려고 할 때

예상치 못한 입력을 처리하는 것은 약간 많을 수 있습니다.

실제로 Python에서 이를 처리하는 훨씬 간단한 방법이 있습니다.

여기에서는 try 및 except 블록을 사용하여 오류를 처리하는 방법을 배웁니다.

먼저 파이썬의 오류가 무엇인지 자세히 알아보겠습니다.

이 과정에서 몇 가지 이유로 인해 오류 메시지가 표시되었습니다.

두 가지 유형으로 나눌 수 있습니다.

구문 오류 및 예외.

Python이 해석할 수 없을 때 구문 오류가 발생합니다.

파이썬에 대한 올바른 구문을 따르지 않았기 때문에 우리의 코드입니다.

오타를 만들 때 얻을 수 있는 오류입니다.

또는 처음으로 Python을 배우기 시작합니다.

여기에서 구문 오류가 발생합니다.

문자열 끝에 있는 끝 인용 부호를 놓쳤습니다.

오류 메시지에서 이것을 볼 수 있습니다.

SyntaxError: 문자열 리터럴을 스캔할 때 줄 끝.

다른 종류의 오류,

예외는 코드 실행 중에 예기치 않은 일이 발생할 때 발생합니다.

여기에서 우리의 코드는 문법적으로 정확하지만,

문자열 철자 때문에 예외가 발생합니다.

10은 int 함수에 대한 유효한 인수가 아닙니다.

Python에는 다양한 유형의 내장 예외가 있습니다.

오류 메시지에서 여기에서 어떤 예외가 발생하는지 확인할 수 있습니다.

값 오류는 예외 유형 중 하나입니다.

내장된 연산이나 기능이 있을 때 발생합니다.

올바른 유형이지만 부적절한 값을 가진 인수가 주어졌습니다.

여기에 예외가 있다는 또 다른 예가 있습니다.

여기서는 아직 정의하지 않은 변수 이름을 참조하려고 합니다.

따라서 이름 오류 예외가 발생합니다.

다시 말하지만, 이 코드는 구문적으로 정확합니다.

하지만 Python이 이 코드 줄에 도달하면

이 변수를 실행하려고 할 때 이 변수의 값을 찾을 수 없습니다.

따라서 예외를 발생시키고 코드를 중지합니다.

Errors And Exceptions

  • Syntax errors occur when Python can’t interpret our code, since we didn’t follow the correct syntax for Python. These are errors you’re likely to get when you make a typo, or you’re first starting to learn Python.
  • Exceptions occur when unexpected things happen during execution of a program, even if the code is syntactically correct. There are different types of built-in exceptions in Python, and you can see which exception is thrown in the error message.
%d