If our code may be a chance of raising checked exception then compulsory we should handle that checked exception either by using try, catch or we have to delegate that responsibility to the caller using throws keyword other wise compile time error saying UnreportedException : XXXException must be caught or declared to be thrown.

Example:

package com.java2learn.exception;
public class ExceptionDemo {

	public static void main(String[] args) {
		Thread.sleep(3000);
	}
}

Output:
Compiletime error:Unhandled exception type InterruptedException.

We can resolve this problem either by using try catch or by using throws keyword as follows

Example:

package com.java2learn.exception;
public class ExceptionDemo {

	public static void main(String[] args) {
		try{
		Thread.sleep(3000);
		}catch(InterruptedException exception){
			System.out.println("catch block");
		}
	}
}

Example:

package com.java2learn.exception;
public class ExceptionDemo {

	public static void main(String[] args)throws InterruptedException {
		Thread.sleep(3000);
	}
}

Hence the main objective of throws keyword is to delegate the responsibilities of exception handling to the caller.

Example:

package com.java2learn.exception;
public class ExcpetionHandDemo {

	public static void main(String[] args) throws InterruptedException{
	
		doStuff();
	}
	private static void doStuff()throws InterruptedException {
		doMoreStuff();
		
	}
	private static void doMoreStuff() throws InterruptedException{
		Thread.sleep(5000);
	
	}
}

If we are not taking at least one throws keyword we will get Compiler Error.

Note:If the parent class constructor throws some checked exception then the child class constructor must throw same checked exception or its parent other wise compiler error.

Example:

package com.java2learn.exception;
public class Parent {

	public Parent()throws Exception {
		
	}
}

class Child extends Parent{
	public Child()throws Exception {
		
	}
}

Note:We can use throws keyword only for Throwable classes other wise Compile time error.

Example:

package com.java2learn.exception;
public class ExceptionDemo {

	public static void main(String[] args)throws ExceptionDemo {
		System.out.println("ExceptionDemo");
	}
}

Output:
Compile time error:No exception of type ExceptionDemo can be thrown; an exception type must be a subclass of Throwable