Note that there are some explanatory texts on larger screens.

plurals
  1. POExecuting custom code inside raised exception
    primarykey
    data
    text
    <p>I've got several methods which can raise my custom exception. After the exception has been raised, I need to handle it, let's say log message to console and save it into database.</p> <p>I was thinking about a crazy workaround described in post title - I could move that custom code with logging and DB-saving into <code>__init__</code> method of my custom exception, so everytime the exception is raised, I would just silence it, since all needed stuff would be done on exception initialization. </p> <p>I'm aware of that the exception <code>__init__</code> itself can raise another exception, but that can be handled too :)</p> <p>Did anyone tried that? </p> <p>And why is it crazy idea? :)</p> <p>-</p> <h1>edit:</h1> <p>I know it's kinda crazy, I'm just curious of your opinion. I will try to enclose what I want to achieve:</p> <p>I'm working with remote data and when communicating to other servers through network, some problems could appear, every one at some other place: 1. network error - on creating connection. 2. HTTP errors (404, 500, etc.) - after connection; 3. remote server can return some other errors too</p> <p>Since these problems occur in several different places, I created my custom exception:</p> <pre><code>class CustomException(Exception): pass </code></pre> <p>and raise it everywhere, when I can catch them, for example:</p> <pre><code>try: conn.open(url) except HTTPException as e: raise CustomException('http') </code></pre> <p>That's just a pseudo-example.</p> <p>This CustomException is catched somewher higher and in almost every place I handle this the same way, i.e.:</p> <pre><code>try: place.populate() except CustomException as e: handle_exception(e) return False </code></pre> <p>And <code>handle_exception</code> saves info about problem to database, does other things also like saving <code>place</code>'s object status and acccess date, but always updates the same things.</p> <p>So I was just wondering if putting that <code>handle_exception</code> code inside <code>__init__</code> would be really crazy idea, since it is done everytime the exception is raised.</p> <p>Thanks for your opinions!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload