From 92ad44412f9310f0dee2bc94010a523c0aee28f2 Mon Sep 17 00:00:00 2001 From: Carlos Galindo Date: Mon, 9 Dec 2019 10:11:50 +0000 Subject: [PATCH] finished revision of chapter 2 --- Secciones/background.tex | 104 ++++++++++++++++++--------------------- 1 file changed, 48 insertions(+), 56 deletions(-) diff --git a/Secciones/background.tex b/Secciones/background.tex index 2b2c1cf..af3aa07 100644 --- a/Secciones/background.tex +++ b/Secciones/background.tex @@ -246,7 +246,7 @@ There exist many more, which have been detailed in surveys of the field, such as Exception handling is common in most modern programming languages. It generally consists of a few new instructions used to modify the normal execution flow and later return to it. Exceptions are used to react to an abnormal program behaviour (controlled or not), and either solve the error and continue the execution, or stop the program gracefully. In our work we focus on the Java programming language, so in the following, we describe the elements that Java uses to represent and handle exceptions: \begin{description} - \item[Throwable.] An interface that encompasses all the exceptions or errors + \item[Throwable.] A type that encompasses all the exceptions or errors that may be thrown. Its two main implementations are \texttt{Error} for internal errors in the Java Virtual Machine and \texttt{Exception} for normal errors. The first ones are generally not caught, as they indicate a critical internal error, such as running out of memory, or overflowing the stack. The second kind encompasses the rest of exceptions that occur in Java. All exceptions can be classified as either \textit{unchecked} (those that extend \texttt{RuntimeException} or \texttt{Error}) or @@ -260,48 +260,43 @@ Exception handling is common in most modern programming languages. It generally either the exception is caught or the last method in the stack (the \texttt{main} method) is popped, and the execution of the program ends abruptly. - \carlos{Review stopped here.} \item[try.] This statement contains a block of statements and one or more \texttt{catch} clauses and/or a \texttt{finally} block. - All exceptions thrown in the statements contained or any methods called will be processed by the list of catches. - \item[catch.] Contains two elements: a variable declaration (the type must - be an exception \sergio{exception o exception type?}) and a block of statements to be executed when an - exception of the corresponding type (or a subtype) is thrown. - \textit{catch} clauses are processed sequentially, and if any matches - the type of the thrown exception, its block is executed, and the rest - are ignored. Variable declarations may be of multiple types - \texttt{(T1|T2 exc)}, when two unrelated types of exception must be - caught and the same code executed for both. When there is an inheritance - relationship, the parent suffices.\footnotemark - \item[finally.] Contains a block of statements that will always be executed - if the \textit{try} is entered. It is used to tidy up, for example - closing I/O streams. The \textit{finally} can be reached in two ways: - with an exception pending (thrown in \textit{try} and not captured by - any \textit{catch} or thrown inside a \textit{catch}) or without it - (when the \textit{try} or \textit{catch} block end successfully). After + All exceptions thrown in the statements contained or any methods called will be processed by the list of \texttt{catch} statements. If no \texttt{catch} matches the type of the exception, the exception propagates to the \texttt{try} block that contains the current one, or, in its absence, the method that called the current one. + \item[catch.] Contains two elements: a variable declaration, whose type must extend from \texttt{Throwable}, and a block of statements to be executed when an exception of a matching type is thrown. + The type of a thrown exception $T_1$ matches the type of a \texttt{catch} statement $T_2$ if one of the following is true: (1) $T_1 = T_2$, (2) $T_1~\textnormal{extends}~T_2$, (3) $T_1~\textnormal{extends}~T \wedge T~\textnormal{matches}~T_2$. + \textit{catch} clauses are processed sequentially, although their order does not matter, due to the restriction that each type must be placed after all of its subtypes. + When a matching clause is found, its block is executed and the rest are ignored. + Variable declarations may be of multiple types \texttt{(T1|T2 e)}, when two unrelated types of exception must be caught and the same code executed for both. + If there is an inheritance relationship, the parent suffices.\footnotemark + \item[finally.] Contains a block of statements that will always be executed, no matter what, if the \textit{try} is entered. + It is used to tidy up, for example closing I/O streams. The \texttt{finally} statement can be reached in two ways: + with an exception pending ---thrown in \texttt{try} and not captured by + any \texttt{catch}, or thrown inside a \texttt{catch}--- or without it + (when the \texttt{try} or \texttt{catch} end successfully). After the last instruction of the block is executed, if there is an exception - pending, control will be passed to the corresponding \textit{catch} or + pending, control will be passed to the corresponding \texttt{catch} or the program will end. Otherwise, the execution continues in the next - statement after the \textit{try-catch-finally} block. + statement after the \texttt{try-catch-finally} block. \end{description} -\footnotetext{Introduced in Java 7, see \url{https://docs.oracle.com/javase/7/docs/technotes/guides/language/catch-multiple.html} for more details.} +\footnotetext{Only available from Java 7 onward. For more details, see \url{https://docs.oracle.com/javase/7/docs/technotes/guides/language/catch-multiple.html} (retrieved November 2019).} \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 +popular object--oriented programming languages feature some kind of error system, which 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 +languages. The list of languages to be analysed has been extracted from the results of 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. Except Bash, Assembly, VBA, C and G,\sergio{Bash y companyia no tienen mecanismo de exception handling? o no se parece al de Java? No queda claro en esta frase} the rest of the languages shown -feature an exception system similar to the one appearing in Java. +in the industry. Table~\ref{tab:popular-languages} displays the list and its +source. All languages displayed there feature an exception system similar to Java's, except for Bash, Assembly, VBA, C and Go\footnotemark. + +\footnotetext{PowerShell only features an exception system since version 2.0, released alongside Windows 7.} \begin{table} \begin{minipage}{0.6\linewidth} @@ -341,32 +336,25 @@ feature an exception system similar to the one appearing in Java. \label{tab:popular-languages} \end{table} -\footnotetext{Data from \url{https://insights.stackoverflow.com/survey/2019/\#technology-\_-programming-scripting-and-markup-languages}} +\footnotetext{From a survey on software developers by StackOverflow. Source: \url{https://insights.stackoverflow.com/survey/2019/\#technology-\_-programming-scripting-and-markup-languages} (retrieved November 2019).} 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\sergio{este ``it" se refiere a inheritance? pon algun objeto y elimina algun it porque hay muchos y me lian xD}, 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\sergio{in following o 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 +featuring a \texttt{throw} statement (i.e. \texttt{raise} in Python), \texttt{try-catch}-like +structure, and most include a \texttt{finally} statement that may be appended to \texttt{try} blocks. +The difference resides in the value passed by the exception. +In programming languages with inheritance and polymorphism, the value is restricted to any type that extends a generic error type (e.g. \texttt{Throwable} in Java). The exceptions are filtered using types. +In languages without inheritance, the value is an arbitrary one (e.g. +JavaScript, TypeScript), with the exceptions being filtered using a boolean condition or pattern to be matched (e.g. JavaScript). In both cases +there exists a way to indicate that all possible exceptions should be caught, regardless of type and content. -\footnotetext{Only since version 2.0, released with Windows 7.} - -On the other hand, in \deleted{the other languages } \sergio{``the other languages" es muy vago}\added{those languages that do not offer explicit exception handling mechanisms,} \deleted{there exist a variety of systems that emulate or replace exception handling:}\added{this feature is covered by a variety of systems that emulate or replace their behaviour:} +Regarding the languages that do not offer an exception handling mechanism similar to Java's, error-handling is covered by a variety of systems, which are briefly detailed below. \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 + from the user's ability to check 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 + files and environment variables before exiting the program. In essence, 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 does not proceed and stop the program. This does not replace a fully @@ -391,10 +379,11 @@ On the other hand, in \deleted{the other languages } \sergio{``the other languag 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}. - \begin{example}[User-built exception system in C] \ \\ - \label{fig:exceptions-c} - \begin{minipage}{0.5\linewidth} + \texttt{longjmp}. Example~\ref{exa:exceptions-c} shows this system in action. + \begin{example}[User-built exception handling system in C] \ \\ + \label{exa:exceptions-c} + \begin{figure}[h] + \begin{minipage}{0.45\linewidth} \begin{lstlisting}[language=C] int main() { if (!setjmp(ref)) { @@ -406,7 +395,7 @@ int main() { } \end{lstlisting} \end{minipage} - \begin{minipage}{0.49\linewidth} + \begin{minipage}{0.54\linewidth} \begin{lstlisting}[language=C] double safe_sqrt(double x, int ref) { if (x < 0) @@ -415,7 +404,10 @@ double safe_sqrt(double x, int ref) { } \end{lstlisting} \end{minipage} - In the \texttt{main} function, line 2 will be executed twice: first when + \caption{A simple \texttt{main} method (left) with an emulated \texttt{try-catch} and a method that computes a square root (left), emulating a \texttt{throw} statement if the number is negative.} + \label{fig:exceptions-c} + \end{figure} + Consider Figure~\ref{fig:exceptions-c}: in the \texttt{main} function, line 2 will be executed twice: first when it is normally reached ---returning 0 and continuing in line 3--- and the second when line 3 in \texttt{safe\_sqrt} is run, returning the second argument of \texttt{longjmp}, and therefore entering the else block in the \texttt{main} method. @@ -433,11 +425,11 @@ double safe_sqrt(double x, int ref) { 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\deleted{order} - (Last In--First Out) \added{order}. - \item[Assembly.] Assembly is a representation of machine code, and each computer architecture has its own instruction set, which makes an analysis impossible. In general, though, no unified exception handling is provided. \carlos{complete with more info on kinds of error handling at the processor level or is this out of scope???}\sergio{Si metes una explicacion asi breve que se entienda bien, si va a ser muy tecnico yo pararia aqui. Diria que las excepciones se manejan a nivel de procesador o lo que sea asi por encima y matizao} + instances can be accumulated. When appropriate, they will run in LIFO + (Last In--First Out) order. + \item[Assembly.] Assembly is a representation of machine code, and each computer architecture has its own instruction set, which makes an analysis impossible. In general, though, no unified exception handling is provided: each processor architecture may provide its own system or not. As with previous entries on this list, the exception system can be emulated, in this case with the low-level instructions commonly available in most architectures. \end{description} -\footnotetext{For more details on Go's design choices, see \url{https://golang.org/doc/faq\#exceptions}. \carlos{Possible transformation to citation???}\sergio{No creo que nos vaya a hacer falta. Con el state of the art y la intro tendremos bastantes.}\josep{mantenlo como footnote}} +\footnotetext{For more details on Go's design choices, see \url{https://golang.org/doc/faq\#exceptions} (retrieved November 2019).} % vim: set noexpandtab:tabstop=2:shiftwidth=2:softtabstop=2:wrap