title: Raising and catching exceptions in an expression in Python 3
body: <p>Let me start off by saying: this is to be used for esoteric purposes - not production code. I'm playing around with doing stuff in a single line of Python code, hence my need for expressions and not statements. (<strong>EDIT:</strong> I'm working on mechanically compiling code to single line of (mostly) equivalent Python code, <a href="https://bitbucket.org/ShadowMaster/onelinepython" rel="nofollow">BitBucket - onelinepython</a>. Note it's very work in progress, hence my reticence in initially mentioning it)</p>
  
  <p>I essentially want to do two things:</p>
  
  <ul>
  <li><p>Call a function that raises an exception instance of my choosing something like:</p>
  
  <p>raise_exception(WhateverException())</p></li>
  <li><p>Run a function in an enclosed environment where I can get the exception instance that is raised, if one is raised, and, otherwise, the return value of the function that was called. E.g.:</p>
  
  <p>has_exception, return_or_exception = check_exception(f, param1, param2, ...)</p></li>
  </ul>
  
  <p>Ideally, I want to do this with some default library or built-in function (no matter how much I have to bastardise its intended use). I don't need functions that have the exact same signatures as the examples I provided, just something I can mangle into something close enough. I do have one restriction, though: <strong>no use of eval() or equivalent functions</strong>.</p>
  
  <p><strong>EDIT:</strong> I know I could define my own functions to do this, but then they would still have to follow the restriction that they are a single expression. So solutions that use <code>raise</code> and <code>try</code> inside a function definition are out. Function definitions, raise-statement and try-blocks are unfortunately statements and not expressions.</p>
  
  <p>As for any solutions I've tried. The answer is none yet. The closest I have to an idea of how to solve this is by misusing <code>unittest</code>'s assert functionality, but I think that is a dead-end.</p>
  
  <p><strong>EDIT 2:</strong> To make it clear, I'm fine with using a module or such that uses <code>raise</code>-statements or <code>try</code>-blocks somewhere in its code. My goal is to take some code and turn it into an equivalent single line of code (which includes any helper functions I may be using). But since I want this to work on a default installation of Python I want to only use default libraries.</p>