简体   繁体   English

在python中是否有一种方法,如果我创建一个.py文件然后将导入另一个.py文件,该文件具有所有异常的catch子句,

[英]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引发异常, test2try...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.

相关问题 我有 4-5 个 py 文件,所有这些文件都使用相同的导入库并使用 selenium python 登录,它们是管理导入语句的更好方法吗 - I Have 4-5 py file all of which uses same import libraries and Login using selenium python, Is their a better way for managing the import statement 如何将 python py 文件中的所有类导入另一个 py 文件中的另一个 class? - how can I import ALL classes in a python py file to another class in another py file? 从其他.py文件创建Python对象 - Create Python object from different .py file Python,如何导入.py文件 - Python, how to import a .py file 如果它们位于不同的文件夹中,如何将.py 文件导入不同的.py 文件? - How to import .py file into different .py file if they are in different folders? 是否可以创建一个临时的 python 文件并导入该 .py 文件的类和属性 - Is it possible to create a temporary python file and import the class and attribute of that .py file 如何运行从 Github 下载的 python 项目(有很多带有 .py 的文件)? - How can I run a python project (has many file with .py) which downloaded from Github? Python setup.py导入python文件 - Python setup.py to import python file 如何在所有文件中导入.py文件? - How to import a .py file in all files? 对于Python 3,如何从另一个目录导入.py文件? - For Python 3, how do I import a .py file from another directory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM