Home>

I am a beginner.
About exception handling written in the following code.

In one site, "java.io.IOException is not a subclass of RuntimeException, so unless you use a try statement to catch an exception, you must declare throwing an exception to a higher method." But what is the "higher method" here?

Also, where is exception handling defined specifically?

package info.searchman;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Pra3_1 {
public static void main (String [] args) throws IOException
{
    BufferedReader br = new BufferedReader (
            new InputStreamReader (System.in));
    int x = Integer.parseInt (br.readLine ());
    int y = Integer.parseInt (br.readLine ());
    if (x>y) {
        System.out.println ("x is greater than y");
    }
}
}

I don't understand much about the concept of throw and try-catch.
Sorry for the rudimentary question, but please give me your answer.

  • Answer # 1

    The program that calls main written in Java is the upper program.
    Since it is not Java, there is a high possibility of not saying "method".

    To execute a compiled main written in Java on a general-purpose OS such as Windows or Linux, use thejavacommand, for example. Thejavacommand accepts exceptions that occur in main, but what thejavacommand does depends on the implementation of thejavacommand.

    If it is normal, an error message will be output and the error end status will be returned to the upper program that called thejavacommand.

  • Answer # 2

    The main function is executed from the system, so I think the upper level here is the command line or the shell
    If there is an exception, you will get a message that an exception has occurred.

  • Answer # 3

    If roughly,
    When running a Windows program, Windows is `` higher ''.
    When you run the java program, the java virtual machine is "upper".

  • Answer # 4

    Answer

      

    What is the "higher method" here?

    When the JVM starts, a thread named "main" starts and calls Pra3_1 # main. It seems that the upper method is run () of the "main" thread, but it is not displayed in the stack trace.

      

    Also, where is exception handling defined specifically?

    In this case, exception handling is not described. According to the specification, if the exception is not caught, the thread will die, and the thread will die by outputting a stack trace. When a thread dies, the JVM will exit if no other thread (except the daemon thread) exists.

    Experiment
    Closes System.in and raises an exception.

    public static void main (String [] args) throws IOException {
      try (BufferedReader br = new BufferedReader (new InputStreamReader (System.in))) {}
      BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
      int x = Integer.parseInt (br.readLine ());

    Stack trace

    Exception in thread "main" java.io.IOException: Stream closed
        at java.io.BufferedInputStream.getBufIfOpen (BufferedInputStream.java:170)
        at java.io.BufferedInputStream.read (BufferedInputStream.java:336)
        at sun.nio.cs.StreamDecoder.readBytes (StreamDecoder.java:284)
        at sun.nio.cs.StreamDecoder.implRead (StreamDecoder.java:326)
        at sun.nio.cs.StreamDecoder.read (StreamDecoder.java:178)
        at java.io.InputStreamReader.read (InputStreamReader.java:184)
        at java.io.BufferedReader.fill (BufferedReader.java:161)
        at java.io.BufferedReader.readLine (BufferedReader.java:324)
        at java.io.BufferedReader.readLine (BufferedReader.java:389)
        at info.searchman.Pra3_1.main (Pra3_1.java:17)