What will happen while running the following code?

public class Test implements Runnable {
    public static void main(String[] args) {
        Thread t = new Thread(this);
        try {
            t.start();
        } catch (IOException ioe) {
            System.out.println("IOException");
        }
    }

    public void run() throws IOException {
        File f = new File("file.txt");
        FileWriter fw = new FileWriter(f);
    }
}
Explanation
1. this is not accessible from static methods.
2. IOException is checked exception. Thread.start() is not declared with throws IOException, therefore throwing IOException in catch-block will cause a compilation error.
3. Method run in Runnable is declared as public abstract void run() without throws clause. Therefore classes that implement this interface can't add any throws clause to the declaration of this method.

Слідкуй за CodeGalaxy

Мобільний додаток Beta

Get it on Google Play
Зворотній Зв’язок
Продовжуйте вивчати
тести з Java
Cosmo
Зареєструйся Зараз
або Підпишись на майбутні тести