Jump to content

Talk:Exception handling (programming)

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Z. Patterson (talk | contribs) at 04:51, 3 February 2025 (Regarding the Syntax section: new section). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

I have a concern regarding the pseudocode used in the example. Currently, the section says the following statement.

In its whole, exception handling code might look like this (in Java-like pseudocode):

try {
    line = console.readLine();

    if (line.length() == 0) {
        throw new EmptyLineException("The line read from console was empty!");
    }

    console.printLine("Hello %s!" % line);
}
catch (EmptyLineException e) {
    console.printLine("Hello!");
}
catch (Exception e) {
    console.printLine("Error: " + e.message());
}
else {
    console.printLine("The program ran successfully.");
}
finally {
    console.printLine("The program is now terminating.");
}

The syntax highlight in this section says that it uses C Sharp. Should we instead use C Sharp terminology, where console.printLine(); would be console.writeLine();, and change the "Java-like pseudocode" phrase to "C sharp"?

Alternatively, if we were to write it in Java, it might be as follows.

import java.util.Scanner;
try {
    Scanner line = new Scanner(System.in);

    if (line.length() == 0) {
        throw new EmptyLineException("The line read from console was empty!");
    }

    System.out.println("Hello %s!" % line);
}
catch (EmptyLineException e) {
    System.out.println("Hello!");
}
catch (Exception e) {
    System.out.println("Error: " + e.message());
}
else {
    System.out.println("The program ran successfully.");
}
finally {
    System.out.println("The program is now terminating.");
}

Another way we could reword this is saying that it would be JavaScript-like code, as JavaScript could also fit this appropriately.

try {
    line = prompt();

    if (line.length() == 0) {
        throw EmptyLineException("The line read from console was empty!");
    }

    console.log("Hello %s!" % line);
}
catch (EmptyLineException e) {
    console.log("Hello!");
}
catch (Exception e) {
    console.log("Error: " + e.message());
}
else {
    console.log("The program ran successfully.");
}
finally {
    console.log("The program is now terminating.");
}

Z. Patterson (talk) 04:51, 3 February 2025 (UTC)[reply]