[Tutor] Trapping Interpreter errors as a string

Lindsay Davies Lindsay.Davies@moonshine.co.uk
Tue, 27 Feb 2001 09:39:56 +0000


--============_-1228855651==_ma============
Content-Type: text/plain; charset="us-ascii" ; format="flowed"

It's also worth being aware of the StandardError exception. If you 
don't know which specific exception will be raised, then 
StandardError will catch all raised exceptions, rather than specific 
ones (AFAIK!).

try:
    x = string.atoi("duh")
except StandardError, problem:
    print problem

Best wishes,

Lindsay


On 27/2/01, Remco Gerlich wrote about 'Re: [Tutor] Trapping 
Interpreter errors as a string':
>In Python, you would usually make something like this:
>
>try:
>    x = string.atoi("duh")
>except ValueError, problem:
>    print problem
>   
>This will print "invalid literal for int(): duh" and then continue. You put
>whatever code you need to handle the exception in the except: block. You can
>look at sys.exc_info for more information on the current exception, or
>inspect the traceback and/or print it with functions from the traceback
>module.
>
>If you don't know about exceptions yet, read chapter 8 of the Tutorial.
--============_-1228855651==_ma============
Content-Type: text/html; charset="us-ascii"

<!doctype html public "-//W3C//DTD W3 HTML//EN">
<html><head><style type="text/css"><!--
blockquote, dl, ul, ol, li { margin-top: 0 ; margin-bottom: 0 }
 --></style><title>Re: [Tutor] Trapping Interpreter errors as a
string</title></head><body>
<div>It's also worth being aware of the<font color="#000000">
StandardError</font> exception. If you don't know which specific
exception will be raised, then<font color="#000000">
StandardError</font> will catch all raised exceptions, rather than
specific ones (AFAIK!).</div>
<div><br></div>
<div>try:</div>
<div>&nbsp;&nbsp; x = string.atoi(&quot;duh&quot;)</div>
<div><font color="#000000"><b>except</b> StandardError,
problem:</font></div>
<div><font color="#000000">&nbsp;&nbsp;<b> print</b>
problem</font></div>
<div><br></div>
<div>Best wishes,</div>
<div><br></div>
<div>Lindsay</div>
<div><br></div>
<div><br></div>
<div>On 27/2/01, Remco Gerlich wrote about 'Re: [Tutor] Trapping
Interpreter errors as a string':</div>
<blockquote type="cite" cite>In Python, you would usually make
something like this:<br>
</blockquote>
<blockquote type="cite" cite>try:<br>
&nbsp;&nbsp; x = string.atoi(&quot;duh&quot;)</blockquote>
<blockquote type="cite" cite>except ValueError, problem:<br>
&nbsp;&nbsp; print problem</blockquote>
<blockquote type="cite" cite>&nbsp;&nbsp;<br>
This will print &quot;invalid literal for int(): duh&quot; and then
continue. You put<br>
whatever code you need to handle the exception in the except: block.
You can<br>
look at sys.exc_info for more information on the current exception,
or<br>
inspect the traceback and/or print it with functions from the
traceback</blockquote>
<blockquote type="cite" cite>module.</blockquote>
<blockquote type="cite" cite><br></blockquote>
<blockquote type="cite" cite>If you don't know about exceptions yet,
read chapter 8 of the Tutorial.</blockquote>
</body>
</html>
--============_-1228855651==_ma============--