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 {
        } catch (IOException ioe) {

    public void run() throws IOException {
        File f = new File("file.txt");
        FileWriter fw = new FileWriter(f);
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.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Sign Up Now
or Subscribe for future quizzes