OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Formatting exceptions as Python does

  • Thread starter Thread starter kuzzooroo
  • Start date Start date
K

kuzzooroo

Guest
If I raise an Exception in Python, here's what I get:

Code:
raise Exception("Hello world")
Traceback (most recent call last):

  File "<ipython-input-24-dd3f3f45afbe>", line 1, in <module>
    raise Exception("Hello world")

Exception: Hello world

Note the last line that says Exception: Hello world. Given an Exception (foo = Exception("Hello world")), how can I produce text like this? None of the following work:

Code:
str(foo)
Out[27]: 'Hello world'

repr(foo)
Out[28]: "Exception('Hello world',)"

"{}".format(foo)
Out[29]: 'Hello world'

"{}: {}".format(type(foo), foo)
Out[30]: "<type 'exceptions.Exception'>: Hello world"
<p>If I raise an Exception in Python, here's what I get:</p>

<pre><code>raise Exception("Hello world")
Traceback (most recent call last):

File "<ipython-input-24-dd3f3f45afbe>", line 1, in <module>
raise Exception("Hello world")

Exception: Hello world
</code></pre>

<p>Note the last line that says <code>Exception: Hello world</code>. Given an Exception (<code>foo = Exception("Hello world")</code>), how can I produce text like this? None of the following work:</p>

<pre><code>str(foo)
Out[27]: 'Hello world'

repr(foo)
Out[28]: "Exception('Hello world',)"

"{}".format(foo)
Out[29]: 'Hello world'

"{}: {}".format(type(foo), foo)
Out[30]: "<type 'exceptions.Exception'>: Hello world"
</code></pre>
 

Latest posts

S
Replies
0
Views
1
Safwan Aipuram
S

Online statistics

Members online
0
Guests online
4
Total visitors
4
Top