question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Propagate exception flag

See original GitHub issue

I would like to know if there is a way that I can propagate an exception instead of catch and print it?

I would expect that this can be some kwarg in the cmd2.__init__ that would allow for the exceptions to be raised instead of printed.

The use case is more for the onecmd functionality. We have a GUI that uses the some built-in commands like run_script and some other custom commands that are only implemented in the cmd2 based application. The exceptions should be caught and displayed by the GUI rather than hidden in the terminal.

Though this can be dealt with on the application level it, maybe I would not be the only one who wants this as an optional argument to be able to control the exceptions?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
kmvanbruntcommented, Feb 25, 2021

@MrKevinWeiss As you probably saw in onecmd_plus_hooks(), cmd2 already has some custom exception handling for those it doesn’t print. As an alternative to adding a parameter to __init__(), we could add a new cmd2 exception class called Cmd2PassthroughException or something similar. You could use this to wrap whatever exception you want. If onecmd_plus_hooks() catches an exception of this type, it will raise it instead of printing it. The question is whether cmd2 should raise the whole Cmd2PassthroughException or just what’s wrapped within it.

class Cmd2PassthroughException(Exception):
    """Custom exception class that will be raised instead of printed"""
    def __init__(self, *args, wrapped_ex: BaseException):
        self.wrapped_ex = wrapped_data
        super().__init__(*args)

@tleonhardt @anselor What are your thoughts?

0reactions
MrKevinWeisscommented, Mar 3, 2021

Thanks guys!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is it bad practice to add a flag to throw an exception?
Yeah, it's a bad idea. If the method gets into a state where it can't continue or otherwise shouldn't get into it should...
Read more >
c# - Having a flag to indicate if we should throw errors
As for the flag itself, the exceptions flag is typically considered code smell because a user needs to somehow know which mode the...
Read more >
Exceptions - GCC, the GNU Compiler Collection
In practice, this means propagating exceptions should not be swallowed in gratuitous catch(...) blocks. Instead, matching try and catch blocks should have ...
Read more >
Exception specifications (throw, noexcept) (C++)
Exception specifications are a C++ language feature that indicate the programmer's intent about the exception types that can be propagated ...
Read more >
Exceptions and Error Handling, C++ FAQ - Standard C++
How should I handle resources if my constructors may throw exceptions? ... or an array of objects) you need to check that status...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found