By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,853 Members | 2,144 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,853 IT Pros & Developers. It's quick & easy.

How to catch multiple exeption of same type raised in decorator

P: 1
I wonder if there is a way to catch all exceptions caused in one decorator in another decorator and handle these exceptions.

However, before coming up with some sort of convoluted solution to my problem, I figured I would ask the experts and see if there is something I'm missing.

My application looks similar to this:

Input will be given as numGen function. This numGen function adds the input to the random number. In my example I am checking for 10 different random numbers.

Note that the input will also be varying as well as the range of random number generation.

Expand|Select|Wrap|Line Numbers
  1. class MyException(Exception):pass
  3. def handleError(func):
  4.     errors =[]
  5.     def wrapper(arg1):
  6.         try:
  7.             result = func(arg1)
  8.         except MyException as e:
  9.             print errors.append(e)
  10.     return wrapper
  12. def findError(func):
  13.     def wrapper(arg1):
  14.         result = func(arg1)
  15.         print result
  16.         for k, v in result.iteritems():
  17.             error_nr = v % 2
  18.             if error_nr ==0:
  19.                 pass
  20.             elif error_nr > 0:
  21.                 raise MyException
  22.     return wrapper
  24. @handleError
  25. @findError
  26. def numGen(input):
  27.     from random import randint
  28.     result= {}
  29.     errors = []
  30.     for i in range(9):
  31.         j = (randint(0,4))
  32.         result[i] = input + j
  33.         result.update()    # This line does nothing...
  34.     return result
  35. if __name__ == '__main__':
  36.     numGen(4)
Function numGen:

This function take the input and generates a 10 random numbers. 10 random numbers are saved in result dictionary with indices. Find error decorator is called base on the result dictionary.

Decorator 1 findError:

On specific condition, it generates an exception for different results, and exceptions are raised, if error occurred.

Decorator 2 handleError:

Catches all the exceptions caused in previous decorators, append to list. reraise later point.

Problem statement:
1.I am not able to iterate over the dictionary in a try block, So I am not able catch specific error.
2.Is there any way catch all the exception, without looping?

Note: Implementation has to use 2 decorators one for generating errors and catching errors. Because I will have 200- 250 functions, I need to check error for all this functions

Please suggest how do I achieve this.
Feb 3 '16 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.