|
2 | 2 |
|
3 | 3 | \begin{frame}[fragile]
|
4 | 4 | \frametitlecpp[98]{Exceptions}
|
5 |
| - \begin{block}{The concept} |
| 5 | + \begin{block}{Purpose} |
6 | 6 | \begin{itemize}
|
7 | 7 | \item to handle \textit{exceptional} events that happen rarely
|
8 | 8 | \item and cleanly jump to a place where the error can be handled
|
|
30 | 30 | }
|
31 | 31 | \end{cppcode*}
|
32 | 32 | \columnbreak
|
33 |
| - \begin{cppcode*}{fontsize=\small,gobble=2} |
| 33 | + \begin{cppcode*}{fontsize=\small,firstnumber=7,gobble=2} |
34 | 34 | void process_data(file &f) {
|
35 | 35 | ...
|
36 | 36 | if (i >= buffer.size())
|
|
46 | 46 | \begin{block}{Throwing exceptions}
|
47 | 47 | \begin{itemize}
|
48 | 48 | \item objects of any type can be thrown (even e.g.\ \cppinline{int})
|
49 |
| - \begin{itemize} |
50 |
| - \item prefer standard exception classes |
51 |
| - \end{itemize} |
52 |
| - \item throw objects by value |
53 | 49 | \end{itemize}
|
54 | 50 | \end{block}
|
| 51 | + \begin{goodpractice}{Throwing exceptions} |
| 52 | + \begin{itemize} |
| 53 | + \item prefer throwing standard exception classes |
| 54 | + \item throw objects by value |
| 55 | + \end{itemize} |
| 56 | + \end{goodpractice} |
| 57 | + \small |
55 | 58 | \begin{cppcode}
|
| 59 | + #include <stdexcept> |
56 | 60 | void process_data(file& f) {
|
57 | 61 | if (!f.open())
|
58 | 62 | throw std::invalid_argument{"stream is not open"};
|
59 |
| - |
60 | 63 | auto header = read_line(f); // may throw an IO error
|
61 | 64 | if (!header.starts_with("BEGIN"))
|
62 | 65 | throw std::runtime_error{"invalid file content"};
|
63 |
| - |
64 | 66 | std::string body(f.size()); // may throw std::bad_alloc
|
65 | 67 | ...
|
66 | 68 | }
|
67 | 69 | \end{cppcode}
|
68 | 70 | \end{frame}
|
69 | 71 |
|
| 72 | +\begin{frame}[fragile] |
| 73 | + \frametitlecpp[98]{Exceptions} |
| 74 | + \begin{block}{Standard exceptions} |
| 75 | + \begin{itemize} |
| 76 | + \item \cppinline{std::exception}, defined in header \cppinline{<exception>} |
| 77 | + \begin{itemize} |
| 78 | + \item Base class of all standard exceptions |
| 79 | + \item Get error message: \cppinline{virtual const char* what() const;} |
| 80 | + \item Please derive your own exception classes from this one |
| 81 | + \end{itemize} |
| 82 | + \item From \cppinline{<stdexcept>}: |
| 83 | + \begin{itemize} |
| 84 | + \item \cppinline{std::runtime_error}, \cppinline{std::logic_error}, \cppinline{std::out_of_range}, \cppinline{std::invalid_argument}, ... |
| 85 | + \item Store a string: \cppinline{throw std::runtime_error{"msg"}} |
| 86 | + \item You should use these the most |
| 87 | + \end{itemize} |
| 88 | + \item \cppinline{std::bad_alloc}, defined in header \cppinline{<new>} |
| 89 | + \begin{itemize} |
| 90 | + \item Thrown by standard allocation functions (e.g.\ \cppinline{new}) |
| 91 | + \item Signals failure to allocate |
| 92 | + \item Carries no message |
| 93 | + \end{itemize} |
| 94 | + \item ... |
| 95 | + \end{itemize} |
| 96 | + \end{block} |
| 97 | +\end{frame} |
| 98 | + |
70 | 99 | \begin{frame}[fragile]
|
71 | 100 | \frametitlecpp[98]{Exceptions}
|
72 | 101 | \begin{block}{Catching exceptions}
|
73 | 102 | \begin{itemize}
|
74 |
| - \item a catch clause catches an exception of the same or derived type |
| 103 | + \item a catch clause catches exceptions of the same or derived type |
75 | 104 | \item multiple catch clauses will be matched in order
|
76 | 105 | \item if no catch clause matches, the exception propagates
|
77 | 106 | \item if the exception is never caught, \cppinline{std::terminate} is called
|
|
86 | 115 | std::cerr << "Failed to process file: " << e.what();
|
87 | 116 | }
|
88 | 117 | \end{cppcode}
|
| 118 | + \begin{goodpractice}{Catching exceptions} |
| 119 | + \begin{itemize} |
| 120 | + \item Catch exceptions by const reference |
| 121 | + \end{itemize} |
| 122 | + \end{goodpractice} |
89 | 123 | \end{frame}
|
90 | 124 |
|
91 | 125 | \begin{frame}[fragile]
|
92 | 126 | \frametitlecpp[98]{Exceptions}
|
93 | 127 | \begin{block}{Rethrowing exceptions}
|
94 | 128 | \begin{itemize}
|
95 |
| - \item a caught exception can be rethrown |
| 129 | + \item a caught exception can be rethrown inside the catch handler |
96 | 130 | \item useful when we want to act on an error, but cannot handle and want to propagate it
|
97 | 131 | \end{itemize}
|
98 | 132 | \end{block}
|
|
166 | 200 | \end{multicols}
|
167 | 201 | \end{frame}
|
168 | 202 |
|
169 |
| -\begin{frame}[fragile] |
170 |
| - \frametitlecpp[98]{Exceptions} |
171 |
| - \begin{block}{Standard exceptions} |
172 |
| - \begin{itemize} |
173 |
| - \item \cppinline{std::exception}, defined in header \cppinline{<exception>} |
174 |
| - \begin{itemize} |
175 |
| - \item Base class of all standard exceptions |
176 |
| - \item Get error message: \cppinline{virtual const char* what() const;} |
177 |
| - \item Please derive your own exception classes from this one |
178 |
| - \end{itemize} |
179 |
| - \item From \cppinline{<stdexcept>}: |
180 |
| - \begin{itemize} |
181 |
| - \item \cppinline{std::runtime_error}, \cppinline{std::logic_error}, \cppinline{std::out_of_range}, \cppinline{std::invalid_argument}, ... |
182 |
| - \item Store a string: \cppinline{throw std::runtime_error{"msg"}} |
183 |
| - \item You should use these the most |
184 |
| - \end{itemize} |
185 |
| - \item \cppinline{std::bad_alloc}, defined in header \cppinline{<new>} |
186 |
| - \begin{itemize} |
187 |
| - \item Thrown by standard allocation functions (e.g.\ \cppinline{new}) |
188 |
| - \item Signals failure to allocate |
189 |
| - \item Carries no message |
190 |
| - \end{itemize} |
191 |
| - \item ... |
192 |
| - \end{itemize} |
193 |
| - \end{block} |
194 |
| -\end{frame} |
195 |
| - |
196 | 203 | \begin{frame}[fragile]
|
197 | 204 | \frametitlecpp[17]{Exceptions}
|
198 | 205 | \begin{goodpractice}{Exceptions}
|
199 | 206 | \begin{itemize}
|
200 |
| - \item throw exceptions by value, catch them by (const) reference |
201 | 207 | \item use exceptions for \textit{unlikely} runtime errors outside the program's control
|
202 | 208 | \begin{itemize}
|
203 | 209 | \item bad inputs, files unexpectedly not found, DB connection, \ldots
|
204 | 210 | \end{itemize}
|
205 | 211 | \item \textit{don't} use exceptions for logic errors in your code
|
206 | 212 | \begin{itemize}
|
207 |
| - \item consider \cppinline{assert} and tests |
| 213 | + \item use \cppinline{assert} and tests |
208 | 214 | \end{itemize}
|
209 | 215 | \item \textit{don't} use exceptions to provide alternative/skip return values
|
210 | 216 | \begin{itemize}
|
|
305 | 311 |
|
306 | 312 | \begin{frame}[fragile]
|
307 | 313 | \frametitlecpp[11]{noexcept specifier}
|
308 |
| - \begin{block}{} |
| 314 | + \begin{block}{\texttt{noexcept}} |
309 | 315 | \begin{itemize}
|
310 | 316 | \item a function with the \cppinline{noexcept} specifier states that it guarantees to not throw an exception
|
311 | 317 | \begin{cppcode*}{gobble=2,linenos=false}
|
|
314 | 320 | \begin{itemize}
|
315 | 321 | \item either no exceptions is thrown or they are handled internally
|
316 | 322 | \item checked at compile time
|
317 |
| - \item so allows the compiler to optimise around that knowledge |
| 323 | + \item allows the compiler to optimize around that knowledge |
318 | 324 | \end{itemize}
|
319 | 325 | \item a function with \cppinline{noexcept(expression)} is only \cppinline{noexcept} when \cppinline{expression} evaluates to \cppinline{true} at compile-time
|
320 | 326 | \begin{cppcode*}{gobble=2,linenos=false}
|
321 | 327 | int safe_if_8B() noexcept(sizeof(long)==8);
|
322 | 328 | \end{cppcode*}
|
323 |
| - \item Use \cppinline{noexcept} on leaf functions where you know the behaviour |
324 |
| - \item C++11 destructors are \cppinline{noexcept} - never throw from them |
325 | 329 | \end{itemize}
|
326 | 330 | \end{block}
|
| 331 | + \begin{goodpractice}{\texttt{noexcept}} |
| 332 | + \begin{itemize} |
| 333 | + \item Use \cppinline{noexcept} on leaf functions where you know the behavior |
| 334 | + \item C++11 destructors are \cppinline{noexcept} - never throw from them |
| 335 | + \end{itemize} |
| 336 | + \end{goodpractice} |
327 | 337 | \end{frame}
|
328 | 338 |
|
329 | 339 | \begin{advanced}
|
|
0 commit comments