finished first draft of introduction

This commit is contained in:
Carlos Galindo 2019-10-22 11:04:04 +00:00
parent 998d943573
commit 5e5676adc9

View file

@ -164,15 +164,15 @@ dependencies are black, data dependencies red and summary edges blue.
\centering
\begin{minipage}{0.4\linewidth}
\begin{lstlisting}
int multiply(int x, int y) {
int result = 0;
while (x > 0) {
result += y;
x--;
int multiply(int x, int y) {
int result = 0;
while (x > 0) {
result += y;
x--;
}
System.out.println(result);
return result;
}
System.out.println(result);
return result;
}
\end{lstlisting}
\end{minipage}
\begin{minipage}{0.59\linewidth}
@ -262,20 +262,21 @@ consists of the following elements:
\footnotetext{Introduced in Java 7, see \url{https://docs.oracle.com/javase/7/docs/technotes/guides/language/catch-multiple.html} for more details.}
\section{Exception handling in other programming languages}
\subsection{Exception handling in other programming languages}
In almost all programming languages, errors can appear (either through the
developer, the user or the system's fault), and must be dealt with.
Most of the popular object oriented programs feature some kind of error system,
normally very similar to Java's exceptions. In this section, we will perform a
small survey of the error-handling techniques used on the most popular
programming languages. The language list has been extracted from a survey
performed by the programming Q\&A website Stack
developer, the user or the system's fault), and must be dealt with. Most of the
popular object oriented programs feature some kind of error system, normally
very similar to Java's exceptions. In this section, we will perform a small
survey of the error-handling techniques used on the most popular programming
languages. The language list has been extracted from a survey performed by the
programming Q\&A website Stack
Overflow\footnote{\url{https://stackoverflow.com}}. The survey contains a
question about the technologies used by professional developers in their work,
and from that list we have extracted those languages with more than $5\%$ usage
in the industry. Table~\ref{tab:popular-languages} shows the list and its
source.
source. Except Bash, Assembly, VBA, C and G, the rest of the languages shown
feature an exception system similar to the one appearing in Java.
\begin{table}
\begin{minipage}{0.6\linewidth}
@ -317,25 +318,106 @@ source.
\footnotetext{Data from \url{https://insights.stackoverflow.com/survey/2019/\#technology-\_-programming-scripting-and-markup-languages}}
The exception systems that are similar to Java are mostly all the same,
featuring a \texttt{throw} statement (\texttt{raise} in Python), try-catching
structure and most include a finally block that may be appended to try blocks.
The difference resides in the value passed by the exception, which in languages
that feature inheritance it is a class descending from a generic error or
exception, and in languages without it, it is an arbitrary value (e.g.
JavaScript, TypeScript). In object--oriented programming, the filtering is
performed by comparing if the exception is a subtype of the exception being
caught (Java, C++, C\#, PowerShell\footnotemark, etc.); and in languages with
arbitrary exception values, a boolean condition is specified, and the first
catch block that fulfills its condition is activated, in following a pattern
similar to that of \texttt{switch} statements (e.g. JavaScript). In both cases
there exists a way to indicate that all exceptions should be caught, regardless
of type and content.
Most of them feature an exception system similar to the one appearing in Java,
while others (bash, assembly, VBA, C) have no built-in method, but allow
\carlos{todo}. Some
check if the exception is of a given set of types for the catching mechanism
(Java, C++, C\#), whilst others rely on a condition that includes the exception
(Python, JavaScript, TypeScript). All of them have a mechanism that catches all
exceptions ---either by catching the type from which all exceptions inherit or
by providing no condition to check.
\footnotetext{Only since version 2.0, released with Windows 7.}
Go doesn't have an exception system per se, but a simple one can be built by
using the keywords ``panic'' (throw an exception with a value associated),
``defer'' (finally, run even when a panic is activated) and ``recover''
(stopping the panic state, retrieves the value associated with the panic).
Deferred code will be run after the main function ends, before the program
terminates. Each block is stored as a member of a stack, so the execution order
is LIFO. If a panic instruction is run, such code will still run, therefore
acting as a finally. The panic can only be stopped via the ``recover''
instruction, which obtains the value associated with the panic. Then, the
exception
On the other hand, in the other languages there exist a variety of systems that
emulate or replace exception handling:
% vim: set noexpandtab:tabstop=2:sw=2:wrap
\begin{description} % bash, vba, C and Go exceptions explained
\item[Bash] The popular Bourne Again SHell features no exception system, apart
from the user's ability to parse the return code from the last statement
executed. Traps can also be used to capture erroneous states and tidy up all
files and environment variables before exiting the program. Traps allow the
programmer to react to a user or system--sent signal, or an exit run from
within the Bash environment. When a trap is activated, its code run, and the
signal doesn't proceed and stop the program. This doesn't replace a fully
featured exception system, but \texttt{bash} programs tend to be small in
size, with programmers preferring the efficiency of C or the commodities of
other high--level languages when the task requires it.
\item[VBA] Visual Basic for Applications is a scripting programming language
based on Visual Basic that is integrated into Microsoft Office to automate
small tasks, such as generating documents from templates, making advanced
computations that are impossible or slower with spreadsheet functions, etc.
The only error--correcting system it has is the directive \texttt{On Error
$x$}, where $x$ can be 0 ---lets the error crash the program---,
\texttt{Next} ---continues the execution as if nothing had happened--- or a
label in the program ---the execution jumps to the label in case of
error. The directive can be set and reset multiple times, therefore creating
artificial \texttt{try-catch} blocks, but there is no possibility of
attaching a value to the error, lowering its usefulness.
\item[C] In C, errors can also be control via return values, but some of the
instructions it features can be used to create a simple exception system.
\texttt{setjmp} and \texttt{longjmp} are two instructions which set up and
perform inter--function jumps. The first makes a snapshot of the call stack
in a buffer, and the second returns to the position where the buffer was
safe, destroying the current state of the stack and replacing it with the
snapshot. Then, the execution continues from the evaluation of
\texttt{setjmp}, which returns the second argument passed to
\texttt{longjmp}. An example can be seen in figure~\ref{fig:exceptions-c},
where line 2 of the \texttt{main} function will be executed twice, once when
it is normally run (returning 0) and the second when line 3 in
\texttt{safe\_sqrt} is run, returning the second argument of line 3, and
therefore entering the else block in the \texttt{main} method.
\item[Go] The programming language Go is the odd one out in this section, being a
modern programming language without exceptions, though it is an intentional
design decision made by its authors\footnotemark. The argument made was that
exception handling systems introduce abnormal control--flow and complicate
code analysis and clean code generation, as it is not clear the paths that
the code may follow. Instead, Go allows functions to return multiple values,
with the second value typically associated to an error type. The error is
checked before the value, and acted upon. Additionally, Go also features a
simple panic system, with the functions \texttt{panic} ---throws an
exception with a value associated---, \texttt{defer} ---runs after the
function has ended or when a \texttt{panic} has been activated--- and
\texttt{recover} ---stops the panic state and retrieves its value. The
\texttt{defer} statement doubles as catch and finally, and multiple
instances can be accumulated. When appropriate, they will run in LIFO order
(Last In--First Out).
Then, the exception \carlos{complete}
\end{description}
\footnotetext{\url{https://golang.org/doc/faq#exceptions}}
\begin{figure} % example of exception system in C
\centering
\begin{minipage}{0.5\linewidth}
\begin{lstlisting}[language=C]
int main() {
if (!setjmp(ref)) {
res = safe_sqrt(x, ref);
} else {
// Handle error
printf /* ... */
}
}
\end{lstlisting}
\end{minipage}
\begin{minipage}{0.49\linewidth}
\begin{lstlisting}[language=C]
double safe_sqrt(double x, int ref) {
if (x < 0)
longjmp(ref, 1);
return /* ... */;
}
\end{lstlisting}
\end{minipage}
\caption{A user-created exception system in C}
\label{fig:exceptions-c}
\end{figure}
% vim: set noexpandtab:tabstop=2:shiftwidth=2:softtabstop=2:wrap