In Java, is it possible to return more than one value from a catch catch block?

I want to read the identifier, name and account from the console for another use. However, these three variables are inside the scope of the try-catch block. I wonder if there is a way to get these three values ​​from try-catch. ( If I do not want to put the three variables separately in the three pairs of the try-catch block ) Thanks for the time.

Here is my sample code:

    StuManage sm = new StuManage();
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Please enter the student ID: ");
    try {
        String Id = br.readLine();
        String name = br.readLine();
        float score =Float.parseFloat(br.readLine());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
+4
source share
4 answers

Yes. Just declare the method as throws IOExceptioncompletely remove the attempt.

+3
source

yes, you can declare and initialize them as an invalid value

 String id = null;
 String name = null;
 float score = 1.0f;
 try {
       id = br.readLine();
       name = br.readLine();
       score = Float.parseFloat(br.readLine());
 } catch (IOException e) {

try , ...

+2

, , , , .

try/catch try/catch main, ( ) , , , .

0

, try catch , - try, .

-3

Source: https://habr.com/ru/post/1676573/


All Articles