sometimeswhen we catch an exceptionwe need reference to the exception object itself this most often happens when we define our own exceptions with custom argumentsbut can also be relevant with standard exceptions most exception classes accept set of arguments in their constructorand we might want to access those attributes in the exception handler if we define our own exception classwe can even call custom methods on it when we catch it the syntax for capturing an exception as variable uses the as keywordtryraise valueerror("this is an argument"except valueerror as eprint("the exception arguments were" argsif we run this simple snippetit prints out the string argument that we passed into valueerror upon initialization we've seen several variations on the syntax for handling exceptionsbut we still don' know how to execute code regardless of whether or not an exception has occurred we also can' specify code that should be executed only if an exception does not occur two more keywordsfinally and elsecan provide the missing pieces neither one takes any extra arguments the following example randomly picks an exception to throw and raises it then some not-so-complicated exception handling code is run that illustrates the newly introduced syntaximport random some_exceptions [valueerrortypeerrorindexerrornonetrychoice random choice(some_exceptionsprint("raising {}format(choice)if choiceraise choice("an error"except valueerrorprint("caught valueerror"except typeerrorprint("caught typeerror"except exception as eprint("caught some other error%se __class__ __name__)elseprint("this code called if there is no exception"finallyprint("this cleanup code is always called"