[英]Is there a way in python that if i create a .py file and then will import in a different .py file which has catch clauses for all the exceptions,
Is there a way in python that if i create a .py file and then will import in a different .py file which has the catch clauses for all the possible exceptions, in such a way 在python中是否有一种方法,如果我创建一个.py文件,然后将导入另一个.py文件,该文件具有所有可能异常的catch子句,以这种方式
suppose we have a .py file lets say test1 假设我们有.py文件让我们说test1
test1.py: test1.py:
import xyz
x=5;
print x;
func1()
Now we have test2.py ,which has a try block and except caused for all the possible exceptions. 现在我们有了test2.py,它有一个try块,除了导致所有可能的异常。 So what I need is that I want the content of test1.py to come inside the
try
of test2.py .Is there a way either or invoking or importing that I can achieve this? 所以我需要的是,我希望test1.py的内容进入test2.py的
try
。是否有一种方式或者调用或导入我可以实现这一点?
test2.py test2.py
import traceback
import sys
import linecache
# import your file here
try:
import first
# invoke your files main method here and run the module
# it is normal behavior to expect an indentation error if your file and method have not been invoked correctly
except SyntaxError as e:
exc_type, exc_value, exc_traceback = sys.exc_info()
#print(sys.exc_info())
formatted_lines = traceback.format_exc().splitlines()
#print(formatted_lines)
temp=formatted_lines[len(formatted_lines) - 3].split(',')
line_no = str(formatted_lines[len(formatted_lines) - 3]).strip('[]')
line=line_no.strip(',')
#print(line[1])
print " The error method thrown by the stacktrace is " "'" ,e , "'"
print " ********************************************* Normal Stacktrace*******************************************************************"
print(traceback.format_exc())
How about: 怎么样:
test2.py: test2.py:
try:
import test1
except ...:
...
If an exception is raised while importing test1
, the try...except
block in test2
will have a chance to deal with it. 如果在导入
test1
引发异常, test2
的try...except
块将有机会处理它。
Or, you could put the code in test1.py
inside of a main
function: 或者,您可以将
test1.py
的代码放在main
函数中:
def main():
import xyz
x=5;
print x;
func1()
and have test2.py
look like this: 并使
test2.py
看起来像这样:
import test1
try:
import first
# invoke your files main method here and run the module
test1.main()
except SyntaxError as e:
You might want to check out decorators. 您可能想要查看装饰器。 There is a pretty in depth description of them here Understanding Python Decorators .
这里有一个非常深入的描述理解Python装饰器 。
#TEST1.py
from exception_catcher import exception_catcher
@exception_catcher
def testfun1(a,b,c):
d = a+b+c
return d
print testfun1('c','a','t')
print testfun1('c',5,'t')
The decorator class 装饰者类
#exception_catcher.py
class exception_catcher(object):
def __init__(self,f):
self.fun = f
def __call__(self,*args,**kwargs):
try:
return self.fun(*args,**kwargs)
except:
return "THERE WAS AN EXCEPTION"
The command line 命令行
>> python TEST1.py
cat
THERE WAS AN EXCEPTION
Hope that helps. 希望有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.