@@ -309,35 +309,38 @@ def isexceptiontype(exc):
309309 pass
310310 return False
311311
312- def determine_exctype (exc ):
313- if isinstance (exc , BaseException ): # "raise SomeError()"
314- return type (exc )
315- if isexceptiontype (exc ): # "raise SomeError"
316- return exc
317- assert False # Python can only raise stuff that inherits from BaseException
318-
312+ # validate handlers
313+ for excspec , handler in handlers :
314+ if isinstance (excspec , tuple ): # tuple of exception types
315+ if not all (isexceptiontype (t ) for t in excspec ):
316+ raise TypeError ("All elements of a tuple excspec must be exception types, got {}" .format (excspec ))
317+ elif not isexceptiontype (excspec ): # single exception type
318+ raise TypeError ("excspec must be an exception type or tuple of exception types, got {}" .format (excspec ))
319+
320+ # run
319321 try :
320322 ret = body ()
321323 except BaseException as exception :
322- exctype = determine_exctype (exception )
324+ # Even if a class is raised, as in `raise StopIteration`, the `raise` statement
325+ # converts it into an instance by instantiating with no args. So we need no
326+ # special handling for the "class raised" case.
327+ # https://docs.python.org/3/reference/simple_stmts.html#the-raise-statement
328+ # https://stackoverflow.com/questions/19768515/is-there-a-difference-between-raising-exception-class-and-exception-instance/19768732
329+ exctype = type (exception )
323330 for excspec , handler in handlers :
324331 if isinstance (excspec , tuple ): # tuple of exception types
325- if not all (isexceptiontype (t ) for t in excspec ):
326- raise TypeError ("All elements of a tuple excspec must be exception types" )
327332 # this is safe, exctype is always a class at this point.
328333 if any (issubclass (exctype , t ) for t in excspec ):
329334 if accepts_arg (handler ):
330335 return handler (exception )
331336 else :
332337 return handler ()
333- elif isexceptiontype ( excspec ) : # single exception type
338+ else : # single exception type
334339 if issubclass (exctype , excspec ):
335340 if accepts_arg (handler ):
336341 return handler (exception )
337342 else :
338343 return handler ()
339- else :
340- raise TypeError ("excspec must be an exception type or tuple of exception types" )
341344 else :
342345 if elsef is not None :
343346 return elsef ()
0 commit comments