-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexceptions.tex
More file actions
282 lines (241 loc) · 7.58 KB
/
exceptions.tex
File metadata and controls
282 lines (241 loc) · 7.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MUW Presentation
% LaTeX Template
% Version 1.0 (27/12/2016)
%
% License:
% CC BY-NC-SA 4.0 (http://creativecommons.org/licenses/by-nc-sa/3.0/)
%
% Created by:
% Nicolas Ballarini, CeMSIIS, Medical University of Vienna
% nicoballarini@gmail.com
% http://statistics.msi.meduniwien.ac.at/
%
% Customized for UAH by:
% David F. Barrero, Departamento de Automática, UAH
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\documentclass[10pt,compress]{beamer} % Change 10pt to make fonts of a different size
\mode<presentation>
\usepackage[spanish]{babel}
\usepackage{fontspec}
\usepackage{tikz}
\usepackage{etoolbox}
\usepackage{xcolor}
\usepackage{xstring}
\usepackage{listings}
\usepackage{tikz}
\usetikzlibrary{matrix,chains,positioning,decorations.pathreplacing,arrows,shapes}
\usetheme{UAH}
\usecolortheme{UAH}
\setbeamertemplate{navigation symbols}{}
\setbeamertemplate{caption}[numbered]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Presentation Info
\title[Errors and exceptions]{Errors and exceptions}
\author{\asignatura\\\carrera}
\institute{}
\date{Departamento de Automática}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Descomentar para habilitar barra de navegación superior
\setNavigation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Configuración de logotipos en portada
%% Opacidad de los logotipos
\newcommand{\opacidad}{1}
%% Descomentar para habilitar logotipo en pié de página de portada
%\renewcommand{\logoUno}{Images/isg.png}
%% Descomentar para habilitar logotipo en pié de página de portada
%\renewcommand{\logoDos}{Images/CCLogo.png}
%% Descomentar para habilitar logotipo en pié de página de portada
\renewcommand{\logoTres}{Images/ALogo.png}
%% Descomentar para habilitar logotipo en pié de página de portada
%\renewcommand{\logoCuatro}{Images/ELogo.png}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% FOOTLINE
%% Comment/Uncomment the following blocks to modify the footline
%% content in the body slides.
%% Option A: Title and institute
\footlineA
%% Option B: Author and institute
%\footlineB
%% Option C: Title, Author and institute
%\footlineC
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Use this block for a blue title slide with modified footline
{\titlepageBlue
\begin{frame}
\titlepage
\end{frame}
}
\institute{\asignatura}
\begin{frame}[plain]{}
\begin{block}{Objectives}
\begin{enumerate}
\item To be aware of the error handling problem
\item Understand exceptions
\item Handle, create and raise exceptions in Python
\end{enumerate}
\end{block}
\begin{block}{References}
Guido van Rossum, ``\textit{Python Tutorial. Release 3.2.3}'', chapter 8
\end{block}
\end{frame}
{
\disableNavigation{white}
\begin{frame}[shrink]{Table of Contents}
\frametitle{Table of Contents}
\tableofcontents
% You might wish to add the option [pausesections]
\end{frame}
}
\section{Motivation}
\begin{frame}[fragile]{Exceptions}{Motivation}
Errors happen
\begin{itemize}
\item We need a mechanism to handle errors
\item Some errors happen before execution (\textit{syntax errors})
\item Others are only detected in execution (\textit{runtime errors})
\begin{itemize}
\item We need tools to handle errors: Exceptions
\end{itemize}
\end{itemize}
\begin{verbatim}
>>> while True print('Hello world')
File "<stdin>", line 1
while True print('Hello world')
^
SyntaxError: invalid syntax
\end{verbatim}
\begin{verbatim}
>>> int("hola")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'hola'
\end{verbatim}
\end{frame}
\section{Definition}
\begin{frame}[fragile]{Exceptions}{Exception definition (II)}
\alert{Exception}: An error that disrupts the normal execution flow
\begin{itemize}
\item File not found, division by zero, invalid argument, etc
\item Code cannot be executed
\item Elegant solution to handle errors
\end{itemize}
\end{frame}
\begin{frame}{Exceptions}{Exception definition (III)}
\begin{columns}
\column{.50\textwidth}
\centering Call stack
\centering \includegraphics[width=0.8\linewidth]{figs/exceptions-callstack.png}
\column{.50\textwidth}
\textit{Call stack}: Sequence of invoked methods
\end{columns}
\end{frame}
\begin{frame}{Exceptions}{Exception definition (III)}
\begin{columns}
\column{.60\textwidth}
\centering Exception handling
\includegraphics[width=0.8\linewidth]{figs/exceptions-errorOccurs.png}
\column{.60\textwidth}
When an error happens ...
\begin{enumerate}
\item Code execution is stopped
\item An exception is thrown
\item The interpreter goes back in the call stack
\item When the interpreter finds an exception handler, it is executed
\end{enumerate}
The exception handler catches the exception, the program finishes otherwise
\end{columns}
\end{frame}
\begin{frame}{Exceptions}{Exception definition (IV)}
\lstinputlisting{code/stack.txt}
\end{frame}
\section{Handling exceptions}
\begin{frame}{Exceptions}{Handling exceptions (I)}
Handling an exception requires a try-except statement
\begin{itemize}
\item \texttt{try}: Encloses the vulnerable code
\item \texttt{catch}: Code that handles the exception
\end{itemize}
\begin{columns}
\column{.60\textwidth}
\begin{block}{try-catch statement}
\vspace{-0.2cm}
\lstinputlisting{code/try.py}
\vspace{-0.2cm}
\end{block}
\end{columns}
\end{frame}
\begin{frame}{Exceptions}{Handling exceptions (II)}
\begin{columns}
\column{.90\textwidth}
\begin{exampleblock}{try-catch example}
\vspace{-0.2cm}
\lstinputlisting[numbers=left]{code/try-example.py}
\vspace{-0.2cm}
\end{exampleblock}
\end{columns}
\bigskip
The exception type contains the error
\end{frame}
\begin{frame}{Exceptions}{Handling exceptions (III)}
\vspace{-0.2cm}
\begin{columns}
\column{.90\textwidth}
\begin{exampleblock}{try-catch example}
\vspace{-0.2cm}
\lstinputlisting[numbers=left]{code/try-example2.py}
\vspace{-0.2cm}
\end{exampleblock}
\end{columns}
\bigskip
New Python elements
\begin{itemize}
\item Raise
\item Exception as object
\end{itemize}
\end{frame}
\section{Exceptions with arguments}
\begin{frame}{Exceptions}{Exceptions with arguments}
\vspace{-0.2cm}
\textit{Exception arguments}: When we need more info
\vspace{-0.2cm}
\begin{columns}
\column{.70\textwidth}
\begin{exampleblock}{}
\vspace{-0.2cm}
\lstinputlisting[numbers=left]{code/try-arg.py}
\vspace{-0.2cm}
\end{exampleblock}
\vspace{-0.3cm}
\begin{exampleblock}{}
\vspace{-0.2cm}
\lstinputlisting[numbers=left]{code/try-arg-res.py}
\vspace{-0.2cm}
\end{exampleblock}
\end{columns}
\end{frame}
\section{Clean-up actions}
\begin{frame}{Exceptions}{Clean-up actions}
\vspace{-0.2cm}
Sometimes we need to execute code under all circumstances
\begin{itemize}
\item Typically clean-up actions: Close files, database connections, sockets, etc
\item The \textbf{finally} clause solves this problem
\end{itemize}
\vspace{-0.2cm}
\begin{columns}
\column{.60\textwidth}
\begin{block}{Example}
\vspace{-0.2cm}
\lstinputlisting[numbers=left]{code/try-finally.py}
\vspace{-0.2cm}
\end{block}
\end{columns}
\end{frame}
\end{document}