0

I could not get what is wrong with the code ? when I execute nothing happens. I am expecting my custom error message.

def testing():
  try:
    raise Exception('My error!')
  except:
    pass

testing()
2
  • What did you think except: pass means? Commented Sep 30, 2020 at 20:04
  • I wanted to raise a custom exception. so I did it in try block and left except block to do nothing Commented Oct 1, 2020 at 15:35

2 Answers 2

5

You are raising an exception successfully. But you are catching that with try/except block. So nothing happens unless you describe it in except block.

Sign up to request clarification or add additional context in comments.

Comments

4

You are successfully raising an error. And the try/catch statements sees it, and goes to catch as you have raised an error.

To fully customize errors you can declare them as so:

class CustomError(Exception):
    pass
raise CustomError("An error occurred")

results in

__main__.CustomError: An error occurred

1 Comment

You can also define an __init__ function so that there's no need to pass an error string, the class can provide it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.