Hey guys,

I am writing a GUI program, and one of the routines requires that I choose a file to be my data file. If the data file is chosen correctly, then my program will send it to a subroutine which will perform a bunch of methods and ultimately construct a large matrix and return it.

Obviously, a user can import the wrong file, and when my subroutines act on this, then they are going to error. Because the user can import a bunch of different wrong files, I can't anticipate what error will present itself. For example, IndexError vs. ValueError. Therefore, I'd like to do something like is presented in the following metacode:

Imported file = f

Try:
  Run subroutines on f
Except AllErrorsImaginable:
  Run some complaint statement

Basically, I can't think of a better way of doing this without just putting every error type in the ``AllErrorsImaginable'' block. This doesn't seem very elegant. Any tips?

Hey guys,

I am writing a GUI program, and one of the routines requires that I choose a file to be my data file. If the data file is chosen correctly, then my program will send it to a subroutine which will perform a bunch of methods and ultimately construct a large matrix and return it.

Obviously, a user can import the wrong file, and when my subroutines act on this, then they are going to error. Because the user can import a bunch of different wrong files, I can't anticipate what error will present itself. For example, IndexError vs. ValueError. Therefore, I'd like to do something like is presented in the following metacode:

Imported file = f

Try:
  Run subroutines on f
Except AllErrorsImaginable:
  Run some complaint statement

Basically, I can't think of a better way of doing this without just putting every error type in the ``AllErrorsImaginable'' block. This doesn't seem very elegant. Any tips?

Use except Exception . The except statement catches exceptions from derived classes, and almost all exception classes in python are subclasses of Exception.
Also the traceback module can help you with the complaint statements (for example you can call traceback.print_tb())

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.