Exception handling syntax: Difference between revisions

Content deleted Content added
[[C++]]: <source>
Line 99:
 
=== [[D programming language|D]] ===
<source lang=d>
<font color="blue">import</font> std.stdio; <font color="green">// for writefln()</font>
import std.stdio; // for writefln()
<font color="blue">int</font> main() {
int main() {
<font color="blue">try</font> {
try {
<font color="green">// do something that might throw an exception</font>
}
<font color="blue">catch</font> (FooException e) {
<font color="green">// handle exceptions of type FooException</font>
}
<font color="blue">catch</font> (Object o) {
<font color="green">// handle any other exceptions</font>
writefln(<font color="red">"Unhandled exception: "</font>, o);
<font color="blue">return</font> 1;
} return 1;
}
<font color="blue">return</font> 0;
return 0;
}
 
</source>
In D, a finally clause or the [[Resource Acquisition Is Initialization|resource acquisition is initialization]] technique can be used to clean up resources in exceptional situations.