Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python : Java throws equivalent in python

Tags:

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:

public void someMethod() throws SomeException {  } 

Python:

@someDecorator  # any way to do? def someMethod():     pass 
like image 810
Nikhil Rupanawar Avatar asked Aug 17 '13 13:08

Nikhil Rupanawar


People also ask

What is the equivalent of pass in Python for Java?

In Java, a single semicolon with nothing in front of it indicates an empty statement. Just like Python's pass, it is used where the syntax requires a statement, but there is nothing to do there.

What is the throws in Java?

The throws keyword in Java is used to declare exceptions that can occur during the execution of a program. For any method that can throw exceptions, it is mandatory to use the throws keyword to list the exceptions that can be thrown.

What is the difference throw and throws in Java?

Throw is a keyword which is used to throw an exception explicitly in the program inside a function or inside a block of code. Throws is a keyword used in the method signature used to declare an exception which might get thrown by the function while executing the code.

What is the advantage of throws in Java?

The Java throws keyword is used to declare an exception. It gives an information to the programmer that there may occur an exception. So, it is better for the programmer to provide the exception handling code so that the normal flow of the program can be maintained.


2 Answers

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:

def throw_me(x):     raise x 

Or even this one:

def call_func(f):     f()  # f could throw any exception 

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

from functools import wraps  class InvalidRaiseException(Exception):     pass  def only_throws(E):     def decorator(f):         @wraps(f)         def wrapped(*args, **kwargs):             try:                 return f(*args, **kwargs)             except E:                 raise             except InvalidRaiseException:                 raise             except Exception as e:                 raise InvalidRaiseException("got %s, expected %s, from %s" % (                     e.__class__.__name__, E.__name__, f.__name__)                 )          return wrapped     return decorator 
@only_throws(ValueError) def func(x):     if x == 1:         raise ValueError     elif x == 2:         raise Exception 
>>> func(0) >>> func(1) ValueError >>> func(2) InvalidRaiseException: got Exception, expected ValueError, from func 
like image 184
Eric Avatar answered Oct 22 '22 06:10

Eric


There is no standard equivalent of this in Python as far as I know, and it's not necessary either. The best you can do is indicate in the docstring what exceptions/errors are raised in what circumstances, and leave it to whoever is using your functions to work out the rest.

In Java, the throws clause is a sort of bookkeeping. For example,

try {     foo(); } catch (IOException ioe) {  } 

doesn't compile unless foo is known to have the potential of throwing an IOException. The analog in Python:

try:     foo() except IOError as ioe:     pass 

compiles regardless. There is no concept of "checked vs unchecked".

like image 39
arshajii Avatar answered Oct 22 '22 05:10

arshajii