1raise Exception('I know Python!') # Don't! If you catch, likely to hide bugs.
2
1def prefill(n,v):
2 try:
3 n = int(n)
4 except ValueError:
5 raise TypeError("{0} is invalid".format(n))
6 else:
7 return [v] * n
1# There are 3 approaches, the first as lvc mentioned is using sys.exit
2sys.exit('My error message')
3
4# The second way is using print, print can write almost anything including an error message
5print >>sys.stderr, "fatal error" # Python 2.x
6print("fatal error", file=sys.stderr) # Python 3.x
7
8# The third way is to rise an exception which I don't like because it can be try-catch
9raise SystemExit('error in code want to exit')
10
11# it can be ignored like this
12try:
13 raise SystemExit('error in code want to exit')
14except:
15 print("program is still open")