Generic error handling example.
1
2
16
17import msparser
18import sys
19
20def main() :
21 file = msparser.ms_datfile()
22
23 if len(sys.argv) > 1 :
24 file.setFileName(sys.argv[1])
25 else :
26 file.setFileName("wrong_name.txt")
27
28 file.read_file()
29
30 if file.isValid() :
31 print("The file has been read and parsed successfully. Congratulations!")
32 else :
33 checkErrorHandler(file)
34
35
36def checkErrorHandler(obj) :
37 print("Last error description ")
38 print("=========================================")
39 print("Error: %s" % obj.getLastErrorString())
40 print("=========================================")
41 print("Testing the error handling... ")
42 print("=========================================")
43
44 err = obj.getErrorHandler()
45 for i in range(1 + err.getNumberOfErrors()) :
46 print("Error number: %d (%d times): %s" % (
47 err.getErrorNumber(i),
48 err.getErrorRepeats(i) + 1,
49 err.getErrorString(i)
50 ))
51
52 obj.clearAllErrors()
53
54
55if __name__ == "__main__":
56 sys.exit(main())
57
58
59""" Running the program with no arguments, for example with
60
61python common_error.py
62
63will give the output:
64
65
66Last error description
67=========================================
68Error: Cannot find Mascot configuration file 'wrong_name.txt'.
69=========================================
70Testing the error handling...
71=========================================
72Error number: 0 (1 times):
73Error number: 1537 (1 times): Cannot find Mascot configuration file 'wrong_name.txt'.
74
75
76"""
77