Talk:Exception handling (programming)

Regarding the Syntax section

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

Javascript doesn't have static types, so the catch (EmptyLineException e) is invalid. The reason both Java and C# are pseudocode is because else is not valid. I would say, if you care about the language, then use Python, it actually has all of the features needed and hence could be real code rather than pseudocode. But as it is, I see no issue with saying Java-like and then using C# for syntax highlighting. Mathnerd314159 (talk) 05:42, 3 February 2025 (UTC)Reply

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.