Lecture 7 Flashcards

exception handling, pdb

1
Q

raise keyword

syntax

A

raise exceptions or errors. The raise keyword raises an error and stops the control flow of the program

raise ExceptionType(“user text”)

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

how do you raise an exception?

A

raise keyword

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

how does try() work? (5)

A

1 First, the try clause is executed i.e. the code between try.
2 If there is no exception, then only the try clause will run, except clause is finished.
3 If any exception occurs, the try clause will be skipped and except clause will run.
4 If any exception occurs, but the except clause within the code doesn’t handle it, it is passed on to the outer try statements. If the exception is left unhandled, then the execution stops.
5 A try statement can have more than one except clause

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

how do you pass a handful of exceptions?

A

by passing a TUPLE of errors into the except block

ex

try:
as;kdjf
except (TypeError, AttributeError)

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

else clause/keyword (2)

A

1 must be present after all the except clauses in the code
2 The code enters the else block ONLY if the try clause does not raise an exception

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

finally clause/keyword (2)

A

1 always executed AFTER the try and except blocks.
2 finally block always executes AFTER the normal termination of the try block OR after the try block terminates due to some exceptions.

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

what is pdb?

A

1 module that comes built-in to the Python standard library
2 actually defined as the class Pdb, which internally makes use of bdb basic debugger functions and cmd (support for line-oriented command interpreters) modules
3 stands for python debugger

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

what are the common pdb commands? (4)

A

l (list) > displays where the last thing that was running.
We can type in the names of the variables to check what they actually contain.
n (next line)
p (print)
c (continue - finishes debugging)

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

what is the major advantage of pdb?

A

it runs purely in the command line, thereby making it great for debugging code on remote servers when we don’t have the privilege of a GUI-based debugger.

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

what is one potential issue of pdb and how do you solve it?

A

Sometimes the variable names might conflict with the pdb commands
For example, in the example below, c is the name of a variable and also a pdb command so it quits instead of displaying the value of the variable

This problem can be solved by adding ‘p’ before the name of the variable, here p stands for print

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

types of errors in python (10)

A

1 Syntax
2 TypeError
3 NameError
4 IndexError
5 KeyError
6 ValueError
7 AttributeError
8 IOError
9 ZeroDivisionError
10 ImportError

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

SyntaxError

A

raised from program syntax issue such as a misspelled keyword, a missing colon, or an unbalanced parenthesis.

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

TypeError

A

raised when an operation or function is applied to an object of the wrong type, such as adding a string to an integer.

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

NameError

A

raised when a variable or function name is not found in the current scope.

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

IndexError

A

raised when an index is out of range for a list, tuple, or other sequence types.

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

KeyError

A

raised when a key is not found in a dictionary.

17
Q

ValueError

A

raised when a function or method is called with an invalid argument or input, such as trying to convert a string to an integer when the string does not represent a valid integer.

18
Q

AttributeError

A

raised when an attribute or method is not found on an object, such as trying to access a non-existent attribute of a class instance.

19
Q

IOError

A

raised when an I/O operation, such as reading or writing a file, fails due to an input/output error.

20
Q

ZeroDivisionError

A

raised when an attempt is made to divide a number by zero.

21
Q

ImportError

A

raised when an import statement fails to find or load a module.

22
Q
A