Google+

Like Us On Facebook


Total Pageviews

234. Using throws in exception handling








There are many examples for explaining the throws keyword. But to explain it clearly, I am using the FileInputStream Class example. First lets understand the FileInputStream Class which later helps us in understanding the throws keyword in exception handling.

FileInputStream Class, uses its methods to read the data from the specified files. FileInputStream Class uses the path of the files for reading the files. We can understand the usage of throws in exception handling by creating an object for FileInputStream Class.

Lets implement on Eclipse IDE -

1. Launch Eclipse IDE, create a new Java Class file 'ThrowsDemo.java' with main( ) method in the existing Java Project 'Project 46' as shown below -



2. Create a text file say 'xyz.txt' on your computer and view the path of the created file as shown below -



3. Convert that path to use it in the Java Program by replacing '\' in the above path with '//'  (i.e. from C:\Users\Arun\Desktop\Files\xyz.txt to C://Users//Arun//Desktop//Files//xyz.txt )

4. Crete an object for FileInputStream Class by using the above converted path as shown below -


5. View the error in the FileInputStream object creation statement as shown below -



6. Select the 'Import 'FileInputStream' (java.io) ' option from the above error message and observe that a new error got displayed as shown below -


7. Observe that in the above compiler error message, 'FileNotFoundException' is displayed even though we have provided the correct file path in the object creation statement.

8. Though we know that this statement wont throw an exception if we run it, but we are not able to run the program because of the compiler error. In this kind of conditions, when we don't want to handle the exceptions using try & catch blocks unnecessarily, we have to use throws keyword beside the method with the suggested exception type to resolve these compiler error by following the below steps.

9. Select 'Add throws declaration' in the compiler error message as shown below -


10. Observe that the error got resolved and throws keyword with the suggested exception type got displayed beside the method name as shown below -



11. throws keyword will help you to get rid of the compiler error. In case, if the file path provided in the object creation statement is not able to find the file, we will get an exception and our program will terminate. So use try & catch blocks to handle the exceptions if you are doubtful about the file path.

12. Now remove the file path from the object creation statement as shown below and observe that no errors are displayed -


13. Save and Run the Java file 'ThrowsDemo.java' and observe that the exception is thrown and the program got terminated by displaying the exception details in the output as shown below -


In this kind of situation use the try catch blocks to handle the exception.


14. Another point to be noted is, Whether throws can be used with Unchecked exceptions like ArithmeticException etc. The answer is Yes, but the program wont throw any compile time error if we have any statements which may throw ArithmeticExceptions. Hence throws keyword is used with the Checked Exceptions mostly.

Example -

Java wont give compiler error for the below statement -

int a = 9/0; //throws arithmetic exception when executed

As we are not getting any compiler error for this statement. We can run the program without handling the exception. Hence there is no need of using throws keyword to resolve any compiler errors as we are not getting any such errors.

But when you are working with checked exceptions like FileNotFoundException etc, compiler by default gives the error messages and user have to use throws keyword beside the method to resolve them. By using throws keyword, we can run the programs without handling the exceptions.

Hence throws keyword need to be used to skip the exception handling, when the compiler makes the exception handling mandatory by giving compiler errors.

So by listing the exceptions that the method might throw in the method's declaration using the throws keyword, the callers of the method can guard themselves against the exception. Hence it is mandatory to list the exceptions given by the compiler in the method's declaration using the throws keyword.

Lets implement this on Eclipse IDE -

1. Launch Eclipse IDE, create a new Java Class 'ThrowsDemo2.java' with main( ) method in the existing Java Project 'Project 46' as shown below -



2. Create a method say 'sample( )' as shown below -


3. Now create an object for FileInputStream Class by providing any File path on your machine as shown below -



4. View the error and resolve it by selecting 'Import FileInputStream (java.io) ' option from the error message as shown below -



5. Observe that another error is displayed after resolving the above error. View the newly displayed error and select 'Add throws declaration' option from the error message to resolve the error as shown below -



6. Observe that the Compiler Suggested Exception Class is added along with throws keyword in the method's declaration as shown below -



7. Now call the sample( ) method from main( ) method as shown below -


8. View the error message and select 'surround with try/catch' option to resolve the error as shown below -


9. Observe that the error got resolved and the try & catch block got surrounded the calling method 'sample( )' as shown below -



Hence in this way we use throws keyword to avoid the exception handling in the method and can handle the exception in the calling method to safeguard it from exceptions. And the calling method can also use throws keyword, if it don't want to handle the exception using try & catch block as shown below -



Hence throws keyword is used in the method's declaration when you don't want to handle the exceptions inside the same method.







Please comment below to feedback or ask questions.

Handling FileNotFoundException will be explained in the next post.




No comments:

Blog Index