Advertisement



< Prev
Next >



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 within, it must use 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
{
}
Here, method1() has declared that it does not catch or handle IOException but only throws it. While, 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


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 fileRead() method, we are performing a file reading operation. File reading operation may throw a checked exception of type FileNotFoundException if file doesn't exist for reading. In 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, using throws keyword, which declares that 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, FileNotFoundException is generated by the code in fileRead() method. Not bound to handle this exception, fileRead() method throws it to the method that called fileRead() method, which in this case is main() method.

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


Advertisement




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"


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

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

When the program runs, method meth() is called from the try block in the main() method. In method meth(), a ParseException is thrown and because meth() method has declared it with throws keyword, this exception is thrown to next method in the call stack, i.e. main() method, where it is handled by the catch block declared with 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

PYTHON


Advertisement