Raising and Handling Exceptions Flashcards

1
Q

Exception

A

An event or error that may cause your program to stop if not properly handled.

“The user provided the wrong data type causing the Traceback to throw a TypeError exception.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Exception Name: NameError

A

A local or global name is not found.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Exception Name: ZeroDivisionError

A

The second argument of a division or modulo operation is zero.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Exception Name: OverflowError

A

The result of an arithmetic operation is too large to be represented.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Exception Name: SyntaxError

A

The parser encounters a syntax error.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Exception Name: TypeError

A

An operation or function is applied to an object of inappropriate type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Exception Name: KeyboardInterrupt

A

The user hits the interrupt key (normally Control-C or Delete).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

TRY and EXPECT

A
we can handle exceptions using the try and except syntax. 
try: 
if x < 0:
raise ValueError(f"x is too small")
^raise generates the exception
except ValueError as problem:
then do something to handle the exception like 
print(:we had an exception"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

try:

and

Body of the try-clause

A

This begins a try-clause. If any exception is raised by any code executed inside the try-clause, the rest of the try-clause is skipped, and code execution moves to the except clause.

The try-clause should include all code that has the possibility of raising an exception that we want to handle. The try-clause is indented once from try:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

except

A

A keyword that begins an except-clause. The except-clause will run if a matching exception is raised from the try-clause.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

ExampleError

A

Replace this with the type of exception that we are handling, such as NameError or ZeroDivisionError.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

as

A

A keyword that designates the caught exception (that matches the type of ExampleError) can be accessed as the local variable to the right.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

error_as_a_variable:

A

Replace this with any valid variable name. This variable’s value will be the exception data, and can be accessed inside of the except-clause. err is a common variable name. Don’t forget the :!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Body of the except clause

A

The error clause should include code that needs to execute if an exception is raised. This code can be used for printing or logging details about err. More powerfully, this is where we can try handling the situation and helping the program move on.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Handling Multiple Types of Exceptions

A

If we need to handle more than one kind of exception, we can add an infinite number of except clauses. Much like the if..elif statements, the raised exception will check if it matches one-at-a-time, starting from the top.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the benefit of using the try…except clause?

A

Raising and using the try…except clause are great ways to make our code more robust by handling errors before they crash our program.

17
Q

pytest Syntax to write a test that expects a raised exception

A

we should determine two things:

1) What function call and input do we expect to raise the exception?
2) What kind of exception do we expect from this function call?

With those two pieces of info in mind, we can understand the pytest syntax:

def test_exceptional_function():
    with pytest.raises(SomeTypeOfException):
         exceptional_function(exceptional_argument)