instantiate an exception first before raising it and add any attributes to it as desired tryraise exception('spam''eggs'except exception as instprint(type(inst)the exception instance print(inst argsarguments stored in args print(inst__str__ allows args to be printed directlybut may be overridden in exception subclasses xy inst args unpack args print(' ='xprint(' =' ('spam''eggs'('spam''eggs' spam eggs if an exception has argumentsthey are printed as the last part ('detail'of the message for unhandled exceptions exception handlers don' just handle exceptions if they occur immediately in the try clausebut also if they occur inside functions that are called (even indirectlyin the try clause for exampledef this_fails() / trythis_fails(except zerodivisionerror as errprint('handling run-time error:'errhandling run-time errordivision by zero raising exceptions the raise statement allows the programmer to force specified exception to occur for exampleraise nameerror('hithere'traceback (most recent call last)file ""line in nameerrorhithere the sole argument to raise indicates the exception to be raised this must be either an exception instance or an exception class ( class that derives from exceptionif an exception class is passedit will be implicitly instantiated by calling its constructor with no argumentsraise valueerror shorthand for 'raise valueerror()if you need to determine whether an exception was raised but don' intend to handle ita simpler form of the raise statement allows you to re-raise the exceptiontryraise nameerror('hithere'except nameerror(continues on next page errors and exceptions