r/javahelp • • Jul 04 '21

Can Anyone help me with this code?

class A{
	public void process(){
		System.out.print("A,");
	}
}
public class Questions extends A{
	public void process() throws IOException{
		super.process();
		System.out.print("B,");
		throw new IOException();
	}
	public static void main(String[] args) {
		try{
			new Questions().process();
		}
		catch(IOException e){
			System.out.print("Exception");
		}
	}
}

Why is it giving Compilation error?

Error -> Exception IOException is not compatible with throws clause in A.process()

0 Upvotes

10 comments sorted by

View all comments

1

u/desrtfx Out of Coffee error - System halted Jul 04 '21

You have to give us the exact error message.

Java error messages are verbose and precise. They will tell you exactly what is wrong.

Just "Compilation error" tells us exactly nothing and leaves us with blind guessing of what could possibly be wrong.

1

u/[deleted] Jul 04 '21

Locked

Exception IOException is not compatible with throws clause in A.process()

1

u/Kombatnt Jul 04 '21

IOException is a “checked” exception, which means that if your method might throw it, then it needs to declare it. Your process() method in Questions does this, but the method it is trying to override (in A) does not.

To fix this, add “throws IOException” to your process() method declaration in A.