|
| 1 | +%% |
| 2 | +%% kit-prog-tutorial |
| 3 | +%% |
| 4 | +%% Slides for my Java programming tutorial at KIT using LaTeX beamer. |
| 5 | +%% |
| 6 | +%% Copyright (c) 2015-2016 YouniS Bensalah <[email protected]> |
| 7 | +%% |
| 8 | +%% This work is released to the public domain. |
| 9 | +%% For the full copyright and license information, please view the LICENSE file. |
| 10 | +%% |
| 11 | + |
| 12 | +\documentclass[18pt]{beamer} |
| 13 | + |
| 14 | +\usepackage{templates/beamerthemekit} |
| 15 | + |
| 16 | +\usepackage[utf8]{inputenc} |
| 17 | +\usepackage{hyperref} |
| 18 | +\usepackage{listings} |
| 19 | +\usepackage{xcolor} |
| 20 | +\usepackage{colortbl} |
| 21 | + |
| 22 | +\titleimage{road} |
| 23 | + |
| 24 | +\definecolor{lime}{HTML}{8FFF53} |
| 25 | + |
| 26 | +\newcommand{\tagline}{Exceptions} |
| 27 | + |
| 28 | +\newcommand{\quotes}[1]{``#1''} |
| 29 | + |
| 30 | +\title[Programmieren\hspace{2.5pt}--\hspace{2.5pt}\tagline]{\tagline} |
| 31 | +\subtitle{Programmieren~\textbar~Tutorium 32} |
| 32 | + |
| 33 | +\author{YouniS Bensalah} |
| 34 | +\date{16. Januar 2017} |
| 35 | + |
| 36 | +\institute{Chair for Software Design and Quality} |
| 37 | + |
| 38 | +\usepackage[citestyle=authoryear,bibstyle=numeric,hyperref,backend=biber]{biblatex} |
| 39 | +\addbibresource{templates/example.bib} |
| 40 | +\bibhang1em |
| 41 | + |
| 42 | +\begin{document} |
| 43 | + |
| 44 | +% remove annoying figure prefix in caption |
| 45 | +\setbeamertemplate{caption}{\raggedright\insertcaption\par} |
| 46 | + |
| 47 | +\selectlanguage{english} |
| 48 | + |
| 49 | +\begin{frame} |
| 50 | + \titlepage |
| 51 | +\end{frame} |
| 52 | + |
| 53 | +% \begin{frame}{Heute} |
| 54 | +% \tableofcontents |
| 55 | +% \end{frame} |
| 56 | + |
| 57 | +\section{Exceptions} |
| 58 | + |
| 59 | +\begin{frame}{Exceptions} |
| 60 | + \begin{block}{} |
| 61 | + \begin{itemize} |
| 62 | + \item \textit{Exceptions} dienen der Ankündigung und Behandlung von \textbf{Ausnahmesituationen} |
| 63 | + (e.g., Fehler, unerwartete Ergebnisse) innerhalb eines Programms |
| 64 | + \item Dabei kann die Information über die Ausnahmesituation an eine andere Programmebene zur Behandlung übergeben werden |
| 65 | + \end{itemize} |
| 66 | + \end{block} |
| 67 | + |
| 68 | +\end{frame} |
| 69 | + |
| 70 | +\begin{frame}{Exceptions} |
| 71 | + \textit{Was passiert jetzt also bei einem Fehler?} |
| 72 | + \vspace{.2in} |
| 73 | + \begin{enumerate} |
| 74 | + \item Das Programm meldet den Vorfall in dem es eine \textbf{Exception} \textit{wirft} |
| 75 | + \item Der \quotes{normale} Kontrollfluss wird unterbrochen und es wird nach einem Zuständigen gesucht, der den Fehler behandelt |
| 76 | + \item Wenn dieser existiert, dann wird die Exception \textit{aufgefangen} und der entsprechende Code wird ausgeführt |
| 77 | + (z. B. der Fehler wird geloggt, das Programm wird terminiert\dots) |
| 78 | + \end{enumerate} |
| 79 | +\end{frame} |
| 80 | + |
| 81 | + |
| 82 | +\subsection{try und catch} |
| 83 | + |
| 84 | + |
| 85 | +\begin{frame}[fragile]{try-catch} |
| 86 | + \begin{itemize} |
| 87 | + \item In den \textbf{try}-Block kommt der Code, der potentiell eine Exception wirft |
| 88 | + \item Im \textbf{catch}-Block steht dann der Code, der die Exception behandelt, falls eine auftritt |
| 89 | + \item Nach dem \textbf{try-catch}-Block steht der Code, welcher anschließend \\ |
| 90 | + \textit{in jedem Fall} ausgeführt wird |
| 91 | + \end{itemize} |
| 92 | + |
| 93 | + \begin{exampleblock}{Syntax} |
| 94 | + \begin{lstlisting}[language=Java,basicstyle=\scriptsize] |
| 95 | +try { |
| 96 | + ... // code that might throw an exception |
| 97 | +} catch (ExceptionType e) { |
| 98 | + ... // code that handles the exception |
| 99 | +} |
| 100 | +... // code that will be executed after try-catch block |
| 101 | + \end{lstlisting} |
| 102 | + |
| 103 | + \end{exampleblock} |
| 104 | +\end{frame} |
| 105 | + |
| 106 | +\subsection{Exception werfen mit throw} |
| 107 | + |
| 108 | + |
| 109 | +\begin{frame}[fragile]{throw} |
| 110 | + \textit{Wann wird eine Exception geworfen?} |
| 111 | + \begin{itemize} |
| 112 | + \item Java wirft einige Exceptions automatisch, wenn etwas zur Laufzeit schiefläuft\dots |
| 113 | + \begin{itemize} |
| 114 | + \item \texttt{Integer.parseInt()} wirft eine \texttt{NumberFormatException}, falls das Argument keine gültige Zahl darstellt |
| 115 | + \end{itemize} |
| 116 | + \item Man kann auch selbst Exceptions werfen ! (mit \texttt{throw}) |
| 117 | + \end{itemize} |
| 118 | + |
| 119 | + \begin{exampleblock}{Syntax} |
| 120 | +throw new SomeException(); |
| 121 | + \end{exampleblock} |
| 122 | + |
| 123 | + \begin{itemize} |
| 124 | + \item Es wird also ein \textbf{Objekt} vom Typ \texttt{SomeException} erstellt und geworfen! |
| 125 | + \end{itemize} |
| 126 | + |
| 127 | +\end{frame} |
| 128 | + |
| 129 | + |
| 130 | +\begin{frame}[fragile]{Exceptions} |
| 131 | + Was wird hier ausgegeben? |
| 132 | + \begin{exampleblock}{} |
| 133 | + \begin{lstlisting}[language=Java,basicstyle=\scriptsize] |
| 134 | +public static void main(String[] args) { |
| 135 | + try { |
| 136 | + doSomething(); |
| 137 | + } catch (ArrayIndexOutOfBoundsException e) { |
| 138 | + System.out.println("Oh noes!"); |
| 139 | + } |
| 140 | + System.out.println("Whatever."); |
| 141 | +} |
| 142 | + |
| 143 | +public static void doSomething() { |
| 144 | + int[] foo = new int[4]; |
| 145 | + foo[4] = 1337; |
| 146 | + System.out.println("I know arrays."); |
| 147 | +} |
| 148 | + \end{lstlisting} |
| 149 | + |
| 150 | + \end{exampleblock} |
| 151 | + |
| 152 | +\end{frame} |
| 153 | + |
| 154 | +\begin{frame}[fragile]{Exceptions} |
| 155 | + \begin{exampleblock}{} |
| 156 | + \begin{lstlisting} |
| 157 | +Oh noes! |
| 158 | +Whatever. |
| 159 | + \end{lstlisting} |
| 160 | + |
| 161 | + \end{exampleblock} |
| 162 | + |
| 163 | + \pause |
| 164 | + |
| 165 | + \begin{itemize} |
| 166 | + \item Die Zeile \begin{lstlisting}[language=Java,basicstyle=\scriptsize] |
| 167 | +foo[4] = 1337; |
| 168 | + \end{lstlisting} |
| 169 | + löst eine \texttt{ArrayIndexOutOfBoundsException} aus |
| 170 | + \item Der Kontrollfluss springt in den \texttt{catch}-Block, der diese Ausnahme abfängt |
| 171 | + \item Anschließend geht es \textbf{\alert{nach dem \texttt{catch}}} weiter! |
| 172 | + \item Die Ausgabe \quotes{\texttt{I know arrays.}} wird nie erreicht |
| 173 | + |
| 174 | + \end{itemize} |
| 175 | + |
| 176 | +\end{frame} |
| 177 | + |
| 178 | + |
| 179 | +\begin{frame}[fragile]{Exceptions} |
| 180 | + Was wird hier ausgegeben? |
| 181 | + \begin{exampleblock}{} |
| 182 | + \begin{lstlisting}[language=Java,basicstyle=\scriptsize] |
| 183 | +public static void main(String[] args) { |
| 184 | + try { |
| 185 | + doMagic(42); |
| 186 | + System.out.println("Okay, I fixed it!"); |
| 187 | + } catch (MagicException e) { |
| 188 | + System.out.println("Wrong magic."); |
| 189 | + } |
| 190 | + System.out.println("Whatever."); |
| 191 | +} |
| 192 | + |
| 193 | +public static void doMagic(int magic) { |
| 194 | + if (magic != 1337) { |
| 195 | + throw new MagicException(); |
| 196 | + } |
| 197 | + System.out.println("Do magic..."); |
| 198 | +} |
| 199 | + \end{lstlisting} |
| 200 | + |
| 201 | + \end{exampleblock} |
| 202 | + |
| 203 | +\end{frame} |
| 204 | + |
| 205 | +\begin{frame}[fragile]{Exceptions} |
| 206 | + \begin{exampleblock}{} |
| 207 | + \begin{lstlisting}[language=Java] |
| 208 | +Wrong magic. |
| 209 | +Whatever. |
| 210 | + \end{lstlisting} |
| 211 | + |
| 212 | + \end{exampleblock} |
| 213 | + |
| 214 | +\end{frame} |
| 215 | + |
| 216 | +\begin{frame}{Exceptions} |
| 217 | + \alert{\huge{\textsc{Nach einer aufgefangenen Exception geht es immer nach catch weiter!!!}}} |
| 218 | +\end{frame} |
| 219 | + |
| 220 | +\subsection{Checked versus Unchecked} |
| 221 | + |
| 222 | + |
| 223 | +\begin{frame}{Eigene Exceptions} |
| 224 | + \begin{itemize} |
| 225 | + \item Man kann die \quotes{built-in} Exceptions von Java verwenden: |
| 226 | + \begin{itemize} |
| 227 | + \item \texttt{IllegalArgumentException} |
| 228 | + \item \texttt{UnsupportedOperationException} |
| 229 | + \item \texttt{IndexOutOfBoundsException} |
| 230 | + \item \texttt{IllegalStateException} |
| 231 | + \item \dots |
| 232 | + \end{itemize} |
| 233 | + \vspace{.2in} |
| 234 | + \item Benötigt man eigene Exceptions, kann man die Klasse \texttt{Exception} bzw. \texttt{RuntimeException} \textbf{erweitern}. |
| 235 | + \end{itemize} |
| 236 | + |
| 237 | +\end{frame} |
| 238 | + |
| 239 | + |
| 240 | +\begin{frame}{Checked vs. Unchecked Exceptions} |
| 241 | + In Java gibt es \textbf{checked} und \textbf{unchecked} Exceptions. |
| 242 | + \vspace{.2in} |
| 243 | + \begin{itemize} |
| 244 | + \item \textbf{Checked Exceptions} |
| 245 | + \begin{itemize} |
| 246 | + \item Ausnahmesituation oder Problem, dessen Ursache \textbf{außerhalb} des Zuständigkeitsbereichs des aktuellen Moduls liegt |
| 247 | + \item \textit{Ungültige Benutzereingabe, Datenbank-Problem, nicht vorhandene Datei \dots} |
| 248 | + \item Müssen \textbf{immer} abgefangen werden und durch \texttt{throws} kenntlich gemacht werden |
| 249 | + \item Unterklassen von \texttt{Exception} |
| 250 | + \end{itemize} |
| 251 | + \end{itemize} |
| 252 | + |
| 253 | +\end{frame} |
| 254 | + |
| 255 | +\begin{frame}{Checked vs. Unchecked Exceptions} |
| 256 | + |
| 257 | + \begin{itemize} |
| 258 | + \item \textbf{Unchecked Exceptions} |
| 259 | + \begin{itemize} |
| 260 | + \item Ausnahmesituation oder ungültiger Zustand, dessen Ursache meistens ein Bug im Programmcode selbst ist |
| 261 | + \item In den meisten Fällen kann das Programm dann ohnehin nicht sinnvoll weiterlaufen |
| 262 | + \item \textit{Ungültige Argumente, division by 0, ungültiger Index bei Zugriff auf Array \dots} |
| 263 | + \item Können, müssen aber nicht abgefangen werden |
| 264 | + \item Unterklassen von \texttt{RuntimeException} |
| 265 | + \end{itemize} |
| 266 | + \end{itemize} |
| 267 | + |
| 268 | +\end{frame} |
| 269 | + |
| 270 | + |
| 271 | +\begin{frame}{Checked vs. Unchecked Exceptions} |
| 272 | + |
| 273 | + \begin{itemize} |
| 274 | + \item \texttt{RuntimeException} ist auch Unterklasse von \texttt{Exception} |
| 275 | + \end{itemize} |
| 276 | + |
| 277 | + |
| 278 | + \vspace{.2in} |
| 279 | + |
| 280 | + \begin{figure} |
| 281 | + \includegraphics[scale=.5]{img/whoa.jpg} |
| 282 | + \end{figure} |
| 283 | + |
| 284 | +\end{frame} |
| 285 | + |
| 286 | +\begin{frame}[fragile]{throws} |
| 287 | + |
| 288 | + \begin{itemize} |
| 289 | + \item Methoden, die \textbf{checked Exceptions} werfen können, müssen mit \texttt{throws} kenntlich gemacht werden |
| 290 | + \end{itemize} |
| 291 | + |
| 292 | + |
| 293 | + \begin{exampleblock}{Syntax} |
| 294 | + \begin{lstlisting}[language=Java,basicstyle=\scriptsize] |
| 295 | +public void foo() throws SomeCheckedException { |
| 296 | + ... |
| 297 | +} |
| 298 | + \end{lstlisting} |
| 299 | + |
| 300 | + \end{exampleblock} |
| 301 | + |
| 302 | +\end{frame} |
| 303 | + |
| 304 | +\begin{frame}{Exceptions} |
| 305 | + \begin{itemize} |
| 306 | + \item Eine \textbf{Exception} ist eine \alert{Ausnahmesituation!} |
| 307 | + \item \textbf{Exceptions} sollten nicht den normalen Kontrollfluss steuern |
| 308 | + \end{itemize} |
| 309 | +\end{frame} |
| 310 | + |
| 311 | +\begin{frame}{Javadoc @throws} |
| 312 | + Alle Exceptions sollten mittels \texttt{@throws} im Javadoc-Kommentar kurz erklärt werden: |
| 313 | + \begin{itemize} |
| 314 | + \item Was bedeutet diese Exception für das Programm |
| 315 | + \item Wann wird sie von der Methode \quotes{geworfen} |
| 316 | + \end{itemize} |
| 317 | +\end{frame} |
| 318 | + |
| 319 | +\appendix |
| 320 | +\beginbackup |
| 321 | + |
| 322 | +\begin{frame}{Fragen?} |
| 323 | + \begin{figure} |
| 324 | + \includegraphics[scale=.6]{img/additionalquestions.jpg} |
| 325 | + \end{figure} |
| 326 | +\end{frame} |
| 327 | + |
| 328 | +\begin{frame}{Bis nächste Woche!} |
| 329 | + \begin{figure} |
| 330 | + \includegraphics[scale=.5]{img/wifi.png} |
| 331 | + \caption{\footnotesize{xkcd.com}} |
| 332 | + \end{figure} |
| 333 | +\end{frame} |
| 334 | + |
| 335 | +\backupend |
| 336 | + |
| 337 | +\end{document} |
0 commit comments