On Tue, Nov 15, 2016 at 3:18 PM yann19 <[email protected]> wrote:

> I have 2 scripts, `script_A` and `script_B` where `script_B` is my main
> code.
> In `B`, I have the following commands...
>
> class dialogManager(QtGui.QDialog):
>     def __init__(self, type="", parent=None):
>         ...
>         things_to_exclude = script_A.get_list_of_exclusions(type)
>         things_to_check = script_A.get_list_of_checked_items(type)
>         ...
>
>
> Generally, the function contents for both `get_list_of_exclusions` and
> `get_list_of_checked_items` are the same with the exception of its name..
> def get_list_of_exclusions(type):
>     file_path = os.path.join(
>         config_path, file_template.format(type=type)
>         )
>     if not os.path.exists(file_path):
>         raise Exception('Config does not exist for the exclusions!'
>                         '\n{0}'.format(file_path)
>                         )
>     with open(file_path) as mapping_file:
>         mapping_data = json.load(file_path)
>     return file_path
>
> Though it may be a bit overkill but I was thinking of writing a check like
> what happens if `type` is not defined or if the developers did not entered
> in a valid `type`...
> class dialogManager(QtGui.QDialog):
>     def __init__(self, type="", parent=None):
>         ...
>         if type == "":
>             raise IOError("Nothing is defined for the type. Please
> contact dev!")
>             sys.exit()
>         things_to_exclude = script_A.get_list_of_exclusions(type)
>         things_to_check = script_A.get_list_of_checked_items(type)
>         if not things_to_exclude or things_to_check:
>             # How do I handle that the Exceptions that were already
> raised, supposed if the files does not exists etc.
>         ...
> But I am stumped at the portion of getting the already raised
> Exceptions... I checked online and derived answers of using try...except..
> is that the only way to do so?
>

First off, I will definitely suggest never calling sys.exit() from library
code (code that gets imported into something else). You most likely don't
want to trigger termination of the process because of a parameter not being
passed correctly.

It should be fine to add an extra check of your "type" parameter if you can
do a more specific check and raise a more specific error message at that
point in time, instead of letting errors bubble up from other calls that
may have less context. Also it can be useful if you want to prevent more
expensive operations from happening when they don't need to, such as
reading from the filesystem only to find out that you had a bad parameter
to begin with. But an IOError is usually reserved for input/output
communication failures such as file system and network reads that are
external to your code. Maybe a ValueError or TypeError would be more
suitable here.

try/except is exactly how you catch, handle, and optionally re-raise
exceptions in Python.

It would look something like:

try:
    get_list_of_exclusions()
except SomeKnownExceptionType as e:
    print "I got an exception:", e
    # re-raise the original exception
    raise

"SomeKnownExceptionType" is the type of exception you expect to see from
calling your function. Raising the generic Exception type in your function
is probably not helpful. It looks like you should be raising an IOError,
which you know will mean a problem with the filesystem (non existant file
in this case).

See this doc on how to raise and catch exception:
https://docs.python.org/2.7/tutorial/errors.html

Justin


> --
> You received this message because you are subscribed to the Google Groups
> "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/b30c4134-210f-44aa-a5c2-31e44204bcb4%40googlegroups.com
> <https://groups.google.com/d/msgid/python_inside_maya/b30c4134-210f-44aa-a5c2-31e44204bcb4%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA3defygTwLOdDefjgcGmokZcnrRaospbj15uddEBm-Dfg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to