Skip to content
Advertisement

Python : Java throws equivalent in python

Not attempting to compare the languages but just for knowledge,

Is there any way to have equivalent of java throws keyword/functionality in Python?

or the way we can recognize checked exception thrown by any method at static time?

or Passing(chaining) exception handling responsibility?

Java:

JavaScript

Python:

JavaScript

Advertisement

Answer

If you can’t have statically typed arguments, you can’t have static throws declarations. For instance, there’s no way for me to annotate this function:

JavaScript

Or even this one:

JavaScript

What you can do is make it an error to throw any type of exception other than those specified:

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement