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
-
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)
Related articles
- i have a question about basic python problems
- java - about the minus of the method
- about android application development in java
- python - about image processing of hog method
- about java overrides
- java - about sql: (colon)
- [java] about equals method (reason why nullpointerexception does not occur even if null is passed/override)
- [java] about variable initialization
- about java version
- java - about screen display in struts2
- i have a question about a simple problem with [java] files
- about the argument of zipentry (java: jdk16)
- java - about nullpointerexception
- java - about char → long type conversion
- java - about localhost
- about the return statement of processing (character string)
- java - processing to call another class from android studio mainactivity [line notify]
- about byte processing clipping in python
- java - about the two arguments of onclick
- java - about the stringutilsisempty () method
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 the
java
command, for example. Thejava
command accepts exceptions that occur in main, but what thejava
command does depends on the implementation of thejava
command.If it is normal, an error message will be output and the error end status will be returned to the upper program that called the
java
command.