30 AUG 2019

exhr.png

Exception: An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions during the execution of a program. When an error occurs within a method, the method creates an object and hands it off to the python runtime. The object, called an exception object, contains information about the error, including its type and the state of the program when the error occurred. Creating an exception object and handing it to the python runtime s called throwing an exception. After a method throws an exception, the python runtime attempts to find something to handle it. The set of possible “somethings” to handle the exception is the ordered list of methods that had been called to get to the method where the error occurred. The try and except block in Python is used to catch and handle exceptions. Python executes code following the try statement as a “normal” part of the program. The code that follows the except statement is the program’s response to any exceptions in the preceding tryclause. You can attach a finally-clause to a try-except block. The code inside the finally clause will always be executed, even if an exception is thrown from within the try or except block. If your code has a return statement inside the try or except block, the code inside the finally-block will get executed before returning from the method. 

Import statement: In Python, modules are accessed by using the import statement. When you do this, you execute the code of the module, keeping the scopes of the definitions so that your current file(s) can make use of these. When Python imports a module called hello for example, the interpreter will first search for a built-in module called hello. If a built-in module is not found, the Python interpreter will then search for a file named hello.py in the current folder 

OS functions:
getcwd – get current working directory
chdir – change current directory 
listdir – print all files in current directory
mkdir – create folder (without sub folder)

More topics covered:

  • try-except-finally
  • Exception tree Hierarchy
  • Inner exception
  • Multiple except statement
  • catch vs except
  • except Exception as e
  • try-except for user input (i.e. int)
  • API class
  • Static method
  • if __name__ == __main__ …

Links:

Leave a comment