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

new on exceptions

parent 9d116f35
\begin{module}[id=python-exceptions]
\importmhmodule[repos=MiKoMH/KWARC,path=missing/en/pseudocode]{pseudocode}
\importmhmodule[path=python/en/nutshell]{python-nutshell}
\lstset{language=python}
\begin{frame}[fragile]
\frametitle{How to deal with Errors in $\pythonLanguage$}
\begin{itemize}
\item<1->
\begin{assertion}[type=theorem,title=Kohlhase's Law]
I can be an idiot, and I do make mistakes!
\end{assertion}
\item<2->
\begin{definition}[id=SML-exception.def]
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.
\end{definition}
\item<3->
\begin{example}[title=Division by Zero]
the $\pythonLanguage$ interpreter reports unhandled \trefis{exception}
\begin{onlyenv}<3>
\begin{lstlisting}
>>> -3 / 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Zerodivisionerror: division by zero
\end{lstlisting}
\end{onlyenv}
\end{example}
\item
\begin{omtext}
Exceptions are first-class citizens in $\pythonLanguage$, in particular they
\begin{itemize}
\item can be defined by the user\lec{classes that inherit from \lstinline|exception|}
\begin{onlyenv}<4>
\begin{lstlisting}
class DivByZero (Exception)
pass
\end{lstlisting}
\end{onlyenv}
\item can be raised when an abnormal condition appears
\begin{onlyenv}<5>
\begin{lstlisting}[mathescape]
if denominator == 0 :
raise DivByZero
else
$\pmetavar{computation}$
\end{lstlisting}
\end{onlyenv}
\item can be handled in a \lstinline|try|/\lstinline|except| block
\begin{onlyenv}<6>
\begin{lstlisting}[mathescape]
try:
$\pmetavar{tentative computation}$
except: $\pmetavar{err}_1$, $\ldots$, $\pmetavar{err}_n$ :
$\pmetavar{errorhandling}$
\end{lstlisting}
\end{onlyenv}
\end{itemize}
\end{omtext}
\end{itemize}
\end{frame}
\end{module}
%%% 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