Commit 5edd07ee authored by Michael Kohlhase's avatar Michael Kohlhase

new on exceptions

parent 9d116f35
\frametitle{How to deal with Errors in $\pythonLanguage$}
\begin{assertion}[type=theorem,title=Kohlhase's Law]
I can be an idiot, and I do make mistakes!
An \defi{exception} is a special $\pythonLanguage$
object. \defii[name=exception-raising]{Raising}{an exception} $e$ terminates
computation and returns $e$ to the next level.
\begin{example}[title=Division by Zero]
the $\pythonLanguage$ interpreter reports unhandled \trefis{exception}
>>> -3 / 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Zerodivisionerror: division by zero
Exceptions are first-class citizens in $\pythonLanguage$, in particular they
\item can be defined by the user\lec{classes that inherit from \lstinline|exception|}
class DivByZero (Exception)
\item can be raised when an abnormal condition appears
if denominator == 0 :
raise DivByZero
\item can be handled in a \lstinline|try|/\lstinline|except| block
$\pmetavar{tentative computation}$
except: $\pmetavar{err}_1$, $\ldots$, $\pmetavar{err}_n$ :
%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:
% LocalWords: importmhmodule theorem,title defii trefis onlyenv
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment