모든 런타임 예외에 대한 처리 (catch 블록)를 제외한 파이썬으로 스크립트를 작성했습니다. 스크립트와 동일한 파일 내에 try 블록을 넣으면 예외가 인쇄되지만 try 블록이 다른 파일에 있으면 스크립트에서 작성된 catch 블록을 사용할 프로 시저가 필요합니다.파이썬에서 예외 처리를위한 라이브러리 파일
마찬가지로 내가 다른 예외를 작성했습니다
import traceback
import sys
import linecache
try:
# execfile(rahul2.py)
def first():
second()
def second():
i=1/0;
def main():
first()
if __name__ == "__main__":
main()
except SyntaxError as e:
exc_type, exc_value, exc_traceback = sys.exc_info()
filename = exc_traceback.tb_frame.f_code.co_filename
lineno = exc_traceback.tb_lineno
line = linecache.getline(filename, lineno)
print("exception occurred at %s:%d: %s" % (filename, lineno, line))
print("**************************************************** ERROR ************************************************************************")
print("You have encountered an error !! no worries ,lets try figuring it out together")
print(" It looks like there is a syntax error in the statement:" , formatted_lines[2], " at line number " , exc_traceback.tb_lineno)
print("Make sure you look up the syntax , this may happen because ")
print(" Remember this is the error message always thrown " "'" ,e , "'")
는 ... 지금은 내 질문에 내가 모든 프로그램이 스크립트를 사용하고자하거나 다른 파일에 try 블록을 가정처럼 생각됩니다 ... 그렇다면 어떻게 내 스크립트와 블록을 시도한 프로그램을 링크 할 수 있습니다 ..
또는 내가 다른 단어를 넣은 다음 try catch 블록이있을 때마다 내가 원하는 것은 catch 블록이 스크립트마다 실행되어야합니다. 내장 라이브러리 대신 ...
코드를 볼 수 있습니까? –
@shikhapanghal : 귀하의 질문을 편집하여 거기에 코드를 넣으십시오 – avasal
질문을 수정했습니다 –