[Tutor] How to raise error without the stack trace

Peter Otten __peter__ at web.de
Sat Nov 26 12:17:46 CET 2011


Karim wrote:

> I want to fire my own exception without having the (useful but ugly in
> my case) stack trace display.

Printing the stack trace is part of the standard exception handling. For an 
uncaught exception python will call sys.excepthook which in turn will print 
the traceback unless you have set it to a custom function.

> How to modify a Exception type class for that purpose which looks like:
> 
> classs MyError(Exception):
>         pass

You don't modify the exception, you handle it explicitly. Wrap your code 
into try ... except:


# before
main() # may raise MyError


# after
try:
    main()
except MyError as e:
    print(e)



More information about the Tutor mailing list