Commits

Jason R. Coombs committed 6d01d1a

Added jaraco.util.exceptions

Comments (0)

Files changed (2)

 * `jaraco.util.logging` now supports ArgumentParser with `add_arguments`
   and `setup`. `add_options` has been replaced with `add_arguments` for
   both OptionParser and ArgumentParser and is deprecated.
+* Added `jaraco.util.exceptions` with a function for determining if a
+  callable throws a specific exception.
 
 3.7
 ~~~

jaraco/util/exceptions.py

+def throws_exception(callable, exception=Exception):
+	"""
+	Return True if the callable throws the specified exception
+	
+	>>> throws_exception(lambda: int('3'))
+	False
+	>>> throws_exception(lambda: int('a'))
+	True
+	>>> throws_exception(lambda: int('a'), KeyError)
+	False
+	"""
+	try:
+		callable()
+	except exception:
+		return True
+	except Exception:
+		pass
+	return False