Exception handling (programming): Difference between revisions

Content deleted Content added
Line 195:
* Scalability: In a hierarchical design, each systems may have several subsystems. Each subsystem may throw several exceptions. Each parent system must deal with the exceptions of all subsystems below it, resulting in an exponential number of exceptions to be dealt with. Checked exceptions require all of these exceptions to be dealt with explicitly.
 
To work around these, Hejlsberg says programmers resort to circumventing the feature by using a {{C++|throws Exception}} declaration. Another circumvention is to use a {{C++|<syntaxhighlight lang="java" inline>try { ... } catch (Exception e) {<nowiki> ... } </nowikisyntaxhighlight> (or even a <syntaxhighlight lang="java" inline>try { ... } catch (Throwable t) { ... } </syntaxhighlight>) handler.<ref name=Trouble/> This is referred to as catch-all exception handling or '''Pokémon exception handling''' after [[Pokémon|the show]]'s catchphrase "[[Pokémon Theme|Gotta Catch 'Em All!]]".<ref>{{cite book |last1=Juneau |first1=Josh |title=Java 9 Recipes: A Problem-Solution Approach |date=31 May 2017 |publisher=Apress |isbn=978-1-4842-1976-8 |page=226 |url=https://books.google.com/books?id=TSYmDwAAQBAJ&pg=PA226 |language=en}}</ref> The Java Tutorials discourage catch-all exception handling as it may catch exceptions "for which the handler was not intended".<ref>{{cite web |url=http://download.oracle.com/javase/tutorial/essential/exceptions/advantages.html |title=Advantages of Exceptions (The Java™ Tutorials : Essential Classes : Exceptions) |publisher=Download.oracle.com |access-date=2011-12-15 |url-status=live |archive-url=https://web.archive.org/web/20111026121217/http://download.oracle.com/javase/tutorial/essential/exceptions/advantages.html |archive-date=2011-10-26 }}</ref> Still another discouraged circumvention is to make all exceptions subclass {{C++|RuntimeException}},<ref>{{cite web|url=http://download.oracle.com/javase/tutorial/essential/exceptions/runtime.html|title=Unchecked Exceptions – The Controversy (The Java™ Tutorials : Essential Classes : Exceptions)|publisher=Download.oracle.com|archive-url=https://web.archive.org/web/20111117042228/http://download.oracle.com/javase/tutorial/essential/exceptions/runtime.html|archive-date=2011-11-17|url-status=live|access-date=2011-12-15}}</ref> thus making the exception unchecked. An encouraged solution is to use a catch-all handler or throws clause but with a specific [[superclass (computer science)|superclass]] of all potentially thrown exceptions rather than the general superclass {{C++|Exception}}. Another encouraged solution is to define and declare exception types that are suitable for the level of abstraction of the called method<ref>Bloch 2001:178 {{cite book | last = Bloch | first = Joshua | year = 2001 | title = Effective Java Programming Language Guide | publisher = Addison-Wesley Professional | isbn = 978-0-201-31005-4 | url-access = registration | url = https://archive.org/details/effectivejavapro00bloc }}</ref> and map lower level exceptions to these types by using [[exception chaining]].
 
=== Similar mechanisms ===
Line 229:
 
An uncaught exceptions analyzer exists for the [[OCaml]] programming language.<ref>{{cite web |url=http://caml.inria.fr/pub/old_caml_site/ocamlexc/ocamlexc.htm |title=OcamlExc - An uncaught exceptions analyzer for Objective Caml |publisher=Caml.inria.fr |access-date=2011-12-15 |url-status=live |archive-url=http://archive.wikiwix.com/cache/20110806090555/http://caml.inria.fr/pub/old_caml_site/ocamlexc/ocamlexc.htm |archive-date=2011-08-06 }}</ref> The tool reports the set of raised exceptions as an extended type signature. But, unlike checked exceptions, the tool does not require any syntactic annotations and is external (i.e. it is possible to compile and run a program without having checked the exceptions).
 
In C++, one can also perform "Pokémon exception handling". Like <syntaxhighlight lang="java" inline>catch (Throwable t)</syntaxhighlight> in Java, C++ supports a <syntaxhighlight lang="cpp">catch (...)</syntaxhighlight> block, which will catch any thrown object. However, <code>catch (...)</syntaxhighlight> has the disadvantage of not naming the caught object, which means it cannot be referred to.
 
<syntaxhighlight lang="cpp">
// Catching only exceptions:
try {
// ...
} catch (const std::exception& e) {
std::println("An exception was caught: {}", e.what());
}
 
// Catching all thrown objects:
try {
// ...
} catch (...) {
std::println("An unknown error was caught");
}
</syntaxhighlight>
 
The [[Rust (programming language)|Rust]] language instead of exceptions altogether, instead represents recoverable exceptions as [[result type]]s. This is represented as <code>Result<T, E></code> (or <code>expected<T, E></code> in C++). The advantage of result types over checked exceptions is that while both result types and checked exceptions force users to immediately handle errors, they can also be directly represented as a return type within the language's type system, unlike checked exceptions where the declared potentially thrown exception is part of the function signature but not directly part of its return type.
 
== Dynamic checking of exceptions ==