3-1-5-13. Handling Errors

If I try to exit the program with Ctrl C,

it doesn’t stop the program because the except handles any error,

including the KeyboardInterrupt error.

We can actually specify which error we want to handle like this.

Now it catches the ValueError exception,

but not other exceptions like KeyboardInterrupt.

Notice in this case when I press Ctrl C,

it ends the program.

This is because a KeyboardInterrupt exception was raised,

which isn’t handled by this try statement.

Also notice that although the program crashed,

this code in the finally block was still executed.

Attempted Input is printed no matter what as the program is exiting this try statement,

even if that means exiting the program.

If we want this handler to address more than one type of exception,

we can include a tuple after the except with the exception like this.

Or if we want to execute different blocks of code depending on the exception,

you can have multiple except blocks like this.

Here, I have two handlers,

one for ValueError and one for KeyboardInterrupt.

When I press Ctrl C,

it says No input taken and breaks from the while loop.

여기서 예외 블록의 코드가 발생합니다.

try 블록을 실행하는 동안 모든 종류의 예외가 발생할 때.

정말로 우리는 ValueError 예외를 조정하고 싶습니다.

Ctrl C로 프로그램을 종료하려고 하면,

예외가 모든 오류를 처리하기 때문에 프로그램을 중지하지 않습니다.

KeyboardInterrupt 오류를 포함하여.

실제로 이와 같이 처리하려는 오류를 지정할 수 있습니다.

이제 ValueError 예외를 포착합니다.

그러나 KeyboardInterrupt와 같은 다른 예외는 아닙니다.

이 경우 Ctrl C를 누르면,

프로그램을 종료합니다.

KeyboardInterrupt 예외가 발생했기 때문입니다.

이 try 문에 의해 처리되지 않습니다.

또한 프로그램이 충돌했지만

finally 블록의 이 코드는 여전히 실행되었습니다.

프로그램이 이 try 문을 종료할 때와 상관없이 시도된 입력이 인쇄됩니다.

그것이 프로그램을 종료하는 것을 의미하더라도.

이 핸들러가 두 가지 이상의 예외 유형을 처리하도록 하려면,

다음과 같은 예외를 제외하고 예외 뒤에 튜플을 포함할 수 있습니다.

또는 예외에 따라 다른 코드 블록을 실행하려는 경우,

이와 같이 여러 개의 예외 블록을 가질 수 있습니다.

여기에 두 개의 핸들러가 있습니다.

하나는 ValueError용이고 하나는 KeyboardInterrupt용입니다.

Ctrl C를 누르면,

아무 입력도 받지 않고 while 루프에서 중단됩니다.

Try Statement

We can use try statements to handle exceptions. There are four clauses you can use (one more in addition to those shown in the video).

  • try: This is the only mandatory clause in a try statement. The code in this block is the first thing that Python runs in a try statement.
  • except: If Python runs into an exception while running the try block, it will jump to the except block that handles that exception.
  • else: If Python runs into no exceptions while running the try block, it will run the code in this block after running the try block.
  • finally: Before Python leaves this try statement, it will run the code in this finally block under any conditions, even if it’s ending the program. E.g., if Python ran into an error while running code in the except or else block, this finally block will still be executed before stopping the program.

Why do we need the finally clause in Python?

Specifying Exceptions

We can actually specify which error we want to handle in an except block like this:

try:
    # some code
except ValueError:
    # some code

Now, it catches the ValueError exception, but not other exceptions. If we want this handler to address more than one type of exception, we can include a parenthesized tuple after the except with the exceptions.

try:
    # some code
except (ValueError, KeyboardInterrupt):
    # some code

Or, if we want to execute different blocks of code depending on the exception, you can have multiple except blocks.

try:
    # some code
except ValueError:
    # some code
except KeyboardInterrupt:
    # some code
%d