-1

I'm trying to display a user-friendly error message while also displaying the exception but I can't seem to make it work.

I've tried these but got invalid syntax errors:

#First try:
except Exception as e, ValueError:
        print("\nThe program is unable to calculate the given equation. " +
            "Try Again!")
        print("\nError message " + e)
        continue

#Second try:
except ValueError, Exception as e:
        print("\nThe program is unable to calculate the given equation. " +
            "Try Again!")
        print("\nError message " + e)
        continue

It would be great if someone could help me out with this. Thanks!

Blue Guy
  • 47
  • 6
  • 1
    Hi, it's a bit unclear what you're trying to achieve with `except Exception as e, ValueError`. Do you want to except all exceptions (`Exception`)? Or just `ValueError`s? (FYI, it's possible to do `except ValueError as e`.) – TrebledJ May 08 '19 at 14:02
  • Thanks for that! Sorry for being unclear. – Blue Guy May 08 '19 at 14:04

1 Answers1

2

You can use traceback:

import traceback

#First try:
except ValueError:
        print("\nThe program is unable to calculate the given equation. " +
            "Try Again!")
        print("\nError message " + traceback.format_exc())
        continue

#Second try:
except Exception as e:
        print("\nThe program is unable to calculate the given equation. " +
            "Try Again!")
        print("\nError message " + traceback.format_exc())
        continue
nacho
  • 4,578
  • 2
  • 17
  • 28