Throws keyword in Java

throws keyword


  • Throws keyword is used to declare an exception.It is one way of informing other programmers that if you call this method you will need to handle the exceptions listed with throws keyword.
  • The throws keyword is used in method declaration, in order to explicitly specify the exceptions that a particular method might throw.
  • When a method declaration has one or more exceptions defined using throws clause then the method-call must handle all the defined exceptions.
We can understand that if a method is declared  with throws clause along with few exceptions then this implicitly tells
other methods that – “ If you call me, you must handle these exceptions that I throw” else compile
time error.
Syntax of Throws in java:
  1. void MethodName() throws ExceptionName
  2. {
  3. Statement1
  4. ...
  5. ...
  6. }
Note: In case a method throws more than one exception, all of them should be listed in throws clause. Below is the example to understand the same.
  1. public void sample() throws IOException, SQLException
  2. {
  3. //Statements
  4. }
If a programmer calls the above method sample(),then he/she has to handle IOException and SQLException
Still not clear?? Read below :
The designer of a class knows very well, the problems that may occur while using the methods or constructors of the class, he designed. If the methods are used by another programmer, the question is how the designer informs the programmer, the possible problems that may occur while using the methods or constructors, he designed. To know this practically, let us observe some method signatures in Java API.
  1. public FileInputStream(String filename) throws FileNotFoundException
  2. public void close()throws IOException
You observe throws exception in the above two signatures. throws is a keyword of Java used by the designer to inform the programmer about the possible problems that may arise during the use of the methods/constructors.This is known as claiming exception. Any exception that comes along throws keyword is a checked exception.When the method is used, if the exception is not handled, the program does not compile.
Let us see one example that does not compile (when exception is not handled) and compiles (when exception is handled). The following program does not compile as the checked exception thrown by the methods is not handled.
  1. import java.io.*;
  2. public class ThrowsProgram
  3. {
  4. public static void main(String args[])
  5. {
  6. FileInputStream fis = new FileInputStream("file.txt");
  7. System.out.println("Hello guide");
  8. }
  9. }
 The above program does not compile because the FileInputStream constructor throws a checked exception, FileNotFoundException. We know earlier, any exception that comes with throws keyword is a checked exception and should be handled, else, the program does not compile.The program does not compile even though the source file "file.txt" is present in same folder.Reason being the FileInputStream constructor throws FileNotFoundException exception which is not handled in the program.It is the robust way of developing a language.
The same previous program is just modified using exception handling mechanism with try-catch blocks. Extra code is bold-faced. Now the program happily compiles.
Exception handling with try-catch        
  1. import java.io.*;
  2. public class ThrowsProgram
  3. {
  4. public static void main(String args[])
  5. {
  6. try
  7. {
  8. FileInputStream fis = new FileInputStream("file.txt");
  9. }
  10. catch(FileNotFoundException e)
  11. {
  12. System.err.println("Source file does not exist sir. " + e);
  13. }
  14. System.out.println("Hello guide");
  15. }
  16. }
As you can observe, the FileInputStream constructor is placed in a try block and FileNotFoundException is handled in the catch block.Hence the program compiles and prints "Hello guide".
The above program is the robust way but few programmers find it tedious to write try and catch block.We have an alternate way without using try and catch block.Below is the program where we are not using try catch block but the program still compiles but this style is not a robust way of programming.
Example on Throws Exception alternative to try-catch block.
  1. import java.io.*;
  2. public class ThrowsProgram
  3. {
  4. public static void main(String args[]) throws FileNotFoundException
  5. {
  6. FileInputStream fis = new FileInputStream("file.txt");
  7. System.out.println("Hello Guide");
  8. }
  9. }
In the above program, instead of try-catch, throws is used in the main() method declaration.This is nothing but rethrowing the exception to the system to handle it. In this type of alternative arrangement, the program compiles and runs happily as long as problem does not arise(when file.txt file exists). If the problem arises (when def.txt file does not exist), the execution terminates simply without executing "Hello Guide". But in earlier program, where try-catch is written, "Hello Guide" prints even if the file does not exist. For this reason, use try-catch instead of throws in real-time programming.

Usage of throws exception in Java

Throws keyword is used in two circumstances in Java.
  • One is claiming the exception where the designer warns the programmer, the possible problems, that may arise when using the method.The second one is as an alternative to try-catch; but not robust way.
  • throws keyword is not used to handle the exception,it is used to declare exceptions.Best way to handle the exceptions is by using try-catch block.Finally block is used for writing clean up code.throws is used to declare exceptions but not to handle.

Comments

Popular posts from this blog

8 Common Programming Mistakes

How CPU Works....!!