0

I have written a Python script that reads in a YAML file and then takes action based on its contents. So far the part where it reads and processes the YAML file works fine. However, I want to make my program robust.

In order to have a robust Python script, the YAML file must first be validated and it must be determined if all the required pieces of data exist inside it and they also contain valid values. At present my program only prints messages on screen if something in YAML file is wrong.

I want to add exception raising and handling feature into my script. Based on my understanding this is the correct way to make program robust. From my reading so far, I have found that we can use the "raise" keyword with an Exception but the examples I have seen so far just use a string containing the error message.

I need to create a mechanism whereby, first the YAML file is analysed and all the problems are stored into a list or dictionary according to their nature. Then, we raise the Exception. Some of these issues will be warnings which are not show stoppers while others will be errors which means that some part of the YAML file cannot be processed at all.

Now if you have read all this, here is my question. How do I create this mechanism where I raise an exception and the data structures containing all the warning and error information is passed along with the exception? Then, the code that catches the exception can look at the information and print messages from them on the screen. If there are errors, the program must be stopped but if there are only warnings then the program can continue.

The exception will be raised only after the entire YAML file has been analysed. I do not want the Python script to raise Exception at the first sight of an error unles the YAML file is broken and cannot be analysed to begin with. The code that catches the exception will have to some quite complex tasks it seems.

Note: I am very new to Python and so far only understand that we have an Exception class and can derive our own exceptions based on this class.

quantum231
  • 2,420
  • 3
  • 31
  • 53
  • 1
    The code underneath the `except SomeYAMLFormatException as ex:` line can contain basically anything you want - you can call functions, use previously-defined variables, create new variables, anything that you can do in a regular Python program, except it should all be in the service of handling the exception. – MattDMo Jun 04 '21 at 21:01
  • 1
    And remember, just because something raises an exception doesn't mean that the whole program has to stop. You can handle the exception smoothly, perhaps print something to the error log, open an error window telling the user what to do next, that sort of thing. It doesn't have to be the end of the world. – MattDMo Jun 04 '21 at 21:03
  • 1
    .. and you can create `class YourOwnException(Exception): ...` wich can have arbritary content - its just a class of which you raise an instance. [proper-way-to-declare-custom-exceptions-in-modern-python](https://stackoverflow.com/questions/1319615/proper-way-to-declare-custom-exceptions-in-modern-python) – Patrick Artner Jun 04 '21 at 21:14
  • Is there some sort of logging module in Python that provides complex functionality that I can use as well? – quantum231 Jun 04 '21 at 21:53
  • I found a logger module in Python, pretty interesting – quantum231 Jun 05 '21 at 00:15
  • I think that the information about the problems in contents of yaml file should go into a log file. Then, if it has errors, I can raise an exception and print contents of the yaml file. – quantum231 Jun 05 '21 at 22:26

0 Answers0