Advertisement



< Prev
Next >



Java - Throws Keyword





According to the Java Compiler - "we must either catch checked exceptions by providing appropriate try-catch block or we should declare them, using throws."

Hence, when a method doesn't want to catch/handle one or more checked exceptions by providing an appropriate try-catch block, it must use the throws keyword in its method signature to declare that it does not handle but only throws such checked exceptions.





Using throws keyword to declare one or multiple checked exceptions


//Declaring one checked exception using throws keyword

public void method1() throws IOException //method signature
{
}



//Declaring multiple checked exception using throws keyword

public void method2() throws FileNotFoundException, ParseException  //method signature
{
}
Over here, the method1() has declared that it does not catch or handle IOException but only throws it. While, the method2() has declared in its signature, that it does not handle either of two checked exceptions separated by commas, i.e. FileNotFoundException and ParseException.




Using throws keyword with one checked exception


//Java - Example of throws keyword

import java.io.*;
class ThrowsExm
{
public static void main(String... ar)
{
	ThrowsExm ob= new ThrowsExm();
	try
	{
		ob.fileRead();
	}	
	catch(FileNotFoundException e)
	{
		System.out.println(e);
	}
} 		//main method ends

public void fileRead() throws FileNotFoundException
{
	File file = new File("D:\\File.txt");
	FileReader fileReader = new FileReader(file); //Checked Exception is thrown here		
}			
}		//class definition ends


Output :


Exception caught - java.io.FileNotFoundException: D:\File.txt (The system cannot find the file specified)  


In the fileRead() method, we are performing a file reading operation and a file reading operation may throw a checked exception of type FileNotFoundException if file doesn't exist for reading. In the fileRead() method, we have not provided a try-catch block to catch/handle such checked exception.

That's why we had to declare the checked exception of type FileNotFoundException with the method signature of fileRead() method, by using the throws keyword, which declares that the fileRead() method is not going to handle an exception of type FileNotFoundException, but will rather pass this exception to the next method in the Call Stack for Exception Handling.

And when the program actually runs, the FileNotFoundException is generated by the code within the fileRead() method. Not bound to handle this exception, the fileRead() method throws it to a method that called the fileRead() method, which in this case is the main() method.

In the main() method, call to the fileRead() method was already guarded inside a try-block and hence, the FileNotFoundException is caught in its catch block with matching FileNotFoundException declaration.


Advertisement




Using throws keyword with multiple checked exceptions


//Java - Using throws keyword with multiple checked exceptions 
 
import java.io.*;
import java.util.*;
import java.text.*;


class A
{
public static void main(String... ar)
{
	A ob= new A();
	try
	{
		ob.writeAndDate();
	}
	catch(Exception e)
	{
		System.out.println("Exception caught - " + e);
	}
} 		//main method ends

public void writeAndDate() throws IOException, ParseException
{
	//Creating a new file and writing a message to it.
	File file = new File("D:\\File.txt");
	FileWriter fw = new FileWriter(file); 
	fw.write("Hello");
	fw.flush();		
	fw.close();

	//Converting  a String to Date object
	DateFormat df = DateFormat.getInstance();
	Date d2= df.parse("100000000L"); //ParseException is thrown, parsing an unparseable String to Date object
	System.out.println(d2);
}			 
}


Output-


Exception caught - java.text.ParseException: Unparseable date: "100000000L"


Note: The IOException could be thrown when a file reading or writing is failed or interrupted, while the ParseException is thrown when an unparsable String is passed to the parse() method of DateFormat class, which tries to parse a String to a Date object.

In the method writeAndDate(), we are first writing to a file, and then we are converting a String to a Date object, hence, the checked exceptions - IOException or ParseException could be thrown by this method and that's why we have used throws keyword to declare these two checked exceptions with the method signature of the writeAndDate() method and this satisfies compiler and our compilation is successful.

When the program runs, the writeAndDate() method is called from the try-block in the main() method. Within the writeAndDate() method, a ParseException is thrown and because the writeAndDate() method has declared it with throws keyword, this exception is thrown to next method in the call stack, i.e. the main() method, where it is handled by a catch block, which is declared with the mother of all exception class, i.e. Exception class.



Please share this article -





< Prev
Next >
< Throws Keyword
User Defined Exception >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement