Error & Exception Handling Flashcards
break (def + ex.)
continue (def + ex.)
pass (def + ex.)
try / except pass
utilized within loops for additional functionality
( 1 )
break: breaks out of the closest enclosing loop
n_string = “sammy”
for letter in n_string:
if letter == ‘a’ :
break
print ( letter )
returns only s then stops running
( 2 )
continue: goes to the top of the closest enclosing loop
n_string = “sammy”
for letter in n_string:
if letter == ‘a’ :
continue
print ( letter )
returns all letters except a in sammy
( 3 )
pass: does nothing, used to create empty functions for later use
( 4 )
try / except pass: skips to the next iteration
Error & Exception Handling Purpose
&
Statement Componenets
(4)
(not all have to be used)
&
Example
Purpose:
allows the script to continue if a user or another programmer executes the code in an unintended way raising an error or exception, used as a wrapper for statements
Statement Components:
try: the attempted block of code
except: block of code allowing executing if an error occurs
else: if no error or execption occurs execute this statement
finally: block of code to be executed, regardless of an error
Example:
def divide(x,y):
try:
result = x // y # floor division
except:
print(“Sorry ! You are dividing by zero”)
finally:
print(“Code that will always execute”)
EOL (end of line)
syntax error: interpreter expected a character at the end of a string literal, but one was not found
EOF (end of file)
syntax error: a function hits the end of the file without reading data
Leading Zero Error
leading zeros are not allowed for integer literals, because of ambiguity. They are are used for hex (0x), octals (0o), and binaries (0b).
Syntax Error
error referring to the structure of the script ex. non closed parenthesis
Runtime Error or Exceptions
defects that manifest only at runtime an interruption that occurs at runtime, forcing the program to stop execution
Semantic Error
statement is syntactically valid, but the code does not do what the programmer intended
Type Error
raised when operation or function is applied to an object of inappropriate type (adding a string and integer together)
OSError
(Definition)
class of built in errors that are invoked when a system related error occurs
there are 15 subclasses of errors that can be invoked depending on the specific error raised (look to python documentation for the list)