0

I want to catch a specific exception and present a message rather than the exception raise and breaking the code.

The exception is:

The exception is: ICSharpCode.Decompiler.Metadata.PEFileNotSupportedException: PE file does not contain any managed metadata.

I know where in the code the exception raises and that I should wrap it with try-except. I don't know how to "Import" the specific exception and then use it properly in the code

I tried to build the exception like this:

class PEFileNotSupportedException(Exception):
    def __init__(self):
        super(PEFileNotSupportedException, self).__init__

and then import it and use it like that:

   try:
       os.system(command)
   except:
       raise PEFileNotSupportedException("The DDL of this type are NOT supported!!!")

But it is not working.

4
  • Please post the error as a text, not an image. And incapsulate it within "```" from both sides so it's formatted proparly Commented Jun 28, 2021 at 14:57
  • What have you written to try and address this issue? Commented Jun 28, 2021 at 14:57
  • Does this answer your question? python exception handling Commented Jun 28, 2021 at 14:58
  • Defining a new exception does not help you catch the original exception. You need to figure out which module exports the exception you want to catch. Commented Jun 28, 2021 at 16:00

2 Answers 2

0
try:
  # your_code_here
except PEFileNotSupportedException:
  print('Your message here')

This should be what you are looking for, assuming you have imported PEFileNotSupportedException

With regards to importing the exception, it should be inside a module and you can import it with the "import" statement.

EDIT: from Module.Where.Your.Exception.Is import PEFileNotSupportedException

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

1 Comment

from Module.Where.Your.Exception.Is I imported it from the file where I created the class and it is not working
0

You could try and import the exception with:

from ICSharpCode.Decompiler.Metadata import PEFileNotSupportedException

try:
    do_something()
except PEFileNotSupportedException:
    print("Exception caught")

1 Comment

Thanks, but there is no such module: from ICSharpCode.Decompiler.Metadata import PEFileNotSupportedException ModuleNotFoundError: No module named 'ICSharpCode'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.