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.