Content deleted Content added
→Java: more info on try-with-resources |
No edit summary Tags: Mobile edit Mobile app edit iOS app edit App section source |
||
(10 intermediate revisions by 7 users not shown) | |||
Line 202:
{{Further|C++}}
<syntaxhighlight lang="cpp">
import std;
int main() {
try {
// do something (might throw an exception)
}▼
// handle a runtime_error e
▲ catch (const std::exception& e) {
} catch (const
// catches all exceptions as e
}▼
} catch (...) {
// catches all
▲ }
}
</syntaxhighlight>
Line 269:
try {
// code which could result in an exception
} catch (any e){
retry;
Line 432 ⟶ 431:
<syntaxhighlight lang="cpp">
import std;
int main() {
try {▼
throw static_cast<int>(42);
▲ try
std::println("(0,{})", e);
std::println("(1,{})", e);
▲ }
}
</syntaxhighlight>
Line 474 ⟶ 473:
}
</syntaxhighlight>
If multiple resources are acquired, the correct way to deal with them is with nested try blocks.<ref>Bloch, Joshua (2018). ''Effective Java, Third Edition''. Addison-Wesley. Item 9, p. 54. {{ISBN|978-0-13-468599-1}}</ref> For this reason and others, ''try-with-resources'' was added to the language to almost entirely replace finally clauses. Resources acquired in a parentheses after the try keyword will be cleaned up automatically. Classes used in these statements must implement an interface called AutoCloseable.<ref>{{cite web | url=https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html | title=The try-with-resources Statement (The Java™ Tutorials > Essential Java Classes > Exceptions) }}</ref>
<syntaxhighlight lang="java">
try (FileReader fr = new FileReader(path);
Line 481 ⟶ 480:
} catch (IOException ioe) {
// Deal with exception.
} //
finally {
// A finally
}
</syntaxhighlight>
Line 494 ⟶ 493:
// Statements in which exceptions might be thrown
throw new Error("error");
} catch (error) {
// Statements that execute in the event of an exception
} finally {
Line 512 ⟶ 511:
try {
throw 12345; // primitive number
} catch (error) {
console.log(error); // logs 12345 as a primitive number to the console
}
Line 521 ⟶ 520:
// Example in Java
try {
Integer i = null;
i.intValue(); // throws a NullPointerException
} catch (NullPointerException error) {
// Variable might be null
} catch (ArithmeticException error) {
// Handle problems with numbers
}
</syntaxhighlight>
Line 535 ⟶ 534:
var example = null;
example.toString();
} catch (error) {
if (error.type === "TypeError") {
// Variable might be null
Line 550 ⟶ 549:
var example = null;
example.toString();
} catch (error) {
if (error.type !== "TypeError") throw error;
// Variable might be null
}
} catch (error) {
if (error.type !== "RangeError") throw error;
// Handle problems with numbers
Line 587 ⟶ 586:
obj.selfPropExample = obj; // circular reference
throw obj;
} catch (error) {
// Statements that execute in the event of an exception
}
</syntaxhighlight>
=== Kotlin ===
{{Further|Kotlin (programming language)}}
<syntaxhighlight lang="kotlin">
try {
// Code that may throw an exception
} catch (e: SomeException) {
// Code for handling the exception
}
</syntaxhighlight>
|