throws Keyword in Java

In java, throws the keyword is used to declare an exception. We know there are two types of exception checked and unchecked Exception. Checked exceptions(compile time) are those which force the programmer to handle it, without handle these exceptions the program doesn’t compile successfully. While unchecked exception(Runtime) doesn’t get checked during compilation.


throws keyword” is mainly used for handling checked exception as using throws we can declare multiple exceptions. It gives an information to the programmer that there may occur an exception so it is better for the programmer to provide the exception handling code so that normal flow can be maintained.


We can declare only checked exceptions because unchecked exceptions are under our control so we can correct our code or handle these exceptions.

Syntax –

type method-name(parameter-list) throws exception-list {

// body of method


Let’s understand this with an easy example-

The output of the above code will be – In displayFile method
at com.Seleniumbix.ExceptionHandling.DemoThrows.displayFile(
at com.Seleniumbix.ExceptionHandling.DemoThrows.main(


Selenium Testing Video Tutorial :


Here an important point to note, If you are calling a method that declares an exception, you must either caught the exception.

In our example code will run whether exception occurs in the display file() method or not.

0 Comment

Leave a Reply