Java. Error missing "; " after throw java. io. IOException


Returns the Error Error:(3, 34) java: ';' expected

When I insert ; writes an error Error:(3, 19) java: cannot find symbol symbol: class io location: package java

I don't understand how to fix it, I'm just learning Java, please direct me to the right path

Code:

class Main {
    public static void main(String args[]) {
        throw java.io.IOException {
            char ch;

            System.out.println("Push");

            ch = (char) System.out.read();
            System.out.println("Now you press " + ch + " button");
        }
    }
}
Author: Arsen, 2019-08-02

1 answers

You just have syntax errors: extra brackets " {}", then instead of throws you have throw, and also for reading from the console use System.in.read(), and you have System.out.read(). That would be correct:

class Main {
    public static void main(String args[]) 
        throws java.io.IOException {
            char ch;
            System.out.println("Push");
            ch = (char) System.in.read();
            System.out.println("Now you press " + ch + " button");        
    }
}

This program reads from the console via the System.in.read() method, which can throw an exception and this exception must be handled somehow. An exception can be raised (they also say "throw", which is a literal translation of throw) using the operator (who would have thought))) throw. This is done by creating an object of type Thowable, or its subclass. The standard signature for creating an object in Java:

throw new java.io.IOException("Demo");// с описанием исключения

As an option:

throw new java.io.IOException(); // без описания исключения

For your example, there are two options.

The first:

public class Main {
    public static void main(String args[]){
        try{
            char ch;
            System.out.println("Push");
            ch = (char)System.in.read();
            System.out.println("Now you press " + ch + " button");
        }catch(java.io.IOException e){
            System.out.println("We have an Error in our programm");
        }        
    }
}

Here, the possible exception occurrence is intercepted and processed directly in this code via the catch operator. That is, in case of an abnormal situation (in this case), the console will display the inscription " We have an Error in our programm".

The second (as you have in the code):

public class Main{
    public static void main(String args[]) throws java.io.IOException{
        char ch;
        System.out.println("Push");
        ch = (char)System.in.read();
        System.out.println("Now you press " + ch + " button");              
    }
}

Here we do not catch a possible exception ourselves, but "throw" the exception above, by adding throws java.io.IOException to the Main method signature, in the Main file, as Sergey Gornostaev pointed out in the comment. I advise you to read carefully about exceptions in Java.

 2
Author: Inwar2006, 2019-08-02 21:05:31