how to use throws in handling Exceptions

As briefed in difference between Checked and Unchecked Exception, unchecked exceptions may or may not be handled. But, be aware that all Checked Exceptions either need to be handled using try and catch blocks or need to be further thrown using throws keyword. However it is responsibility of invokers of the method(which throws Exception) to handle it with try catch blocks, or further throw using throws keyword.

throws keyword need to be used along with method declaration, it just indicates that the method may throw an Exception, and invokers of the method need to either handle it or further throws it.

Also note that multiple Exceptions separated with comma, can be used with throws keyword. For eg

public void show() throws NullPointerException, Exception
{
//..statements
}

This feature, helps third party framework to throw an Exception, and let application developers to handle it appropriately.

Below is an example of throws keyword.


class TDemo1{
	public void met1() throws Exception
	{
	if(true)
	{
		throw new Exception("Some Exception");
	}
	}
}

class TDemo2{
	public void met2() throws Exception,NullPointerException
	{

	TDemo1 td = new TDemo1();
		
	td.met1();
	}
}

public class ThrowsDemo {
	public static void main(String args[]) throws Exception
	{
	try{
	TDemo2 td2 = new TDemo2();
	td2.met2();
		
	}catch(Exception e)
	{
		
	e.printStackTrace();
	//printsline number, class name, file name where the exception has occurred
	//and the methods thru which exception has been propogated, until caught
	throw e;//further rethrowing the exception
	}
	finally{
	System.out.println("dsdsfds");
	}
		
	}
}

You may also like to read:
Create user defined Exception
How to create Unchecked Exception
List of exceptions in java.lang package

Why rethrow an Exception in Java?

What is rethrowing an Exception

When an Exception is thrown from your code,
1. You can handle handle the Exception completely, by using try and catch blocks you handle the Exception, and further throw the Exception object. OR
2. Use throws with method declaration, to indicate that invokers of the method need to handle the Exception, or further throws it.

Another option we have here is a combination of above both. That is, the Exception is handled, with try, catch blocks, and further throw the Exception.

Below is an example, of the same.

class TDemo22{
	public void met2() throws Exception
	{
	try
	{
	String str="2abcd3";

	int i = Integer.parseInt(str);
	}
	catch(Exception ep)
	{
	System.out.println("Exception catching");
	throw ep;
	}
	}
}

public class NumberFormatDemo {
	public static void main(String args[])
	{
	try{
	TDemo22 td2 = new TDemo22();
	td2.met2();

	}catch(Exception e)
	{
	e.printStackTrace();
	}
	}
}

There are two different ways, an Exception can be re thrown.
1. catch an Exception, and further throw same Exception object(as shown in above example)
2. catch an Exception, and further create a new Exception object, and throw it(as shown in below example)

class MyException extends Exception{
}

class TDemo22{
	public void met2() throws Exception
	{
	try
	{
	String str="2abcd3";

	int i = Integer.parseInt(str);
	}
	catch(Exception ep)
	{
	System.out.println("Exception catching");
	throw new MyException(); //re throwing a new Exception object
	}
	}
}

public class NumberFormatDemo {
	public static void main(String args[])
	{
	try{
	TDemo22 td2 = new TDemo22();
	td2.met2();

	}catch(Exception e)
	{
	e.printStackTrace();
	}
	}
}

When rethrowing an Exception may be required?
1. When you want to partially handle Exception situation, and further let invokers of your method, to handle the Exception.
2. When you want to hide class name of your internal exception, and rethrow another Exception.

You may also like to read:
How to print stack trace to a log file
List of inbuilt exception in Java
What is RuntimeException
How to create your own Exception?

Difference between final, finally, finalize()

Below is difference between final, finally and finalize() is entirely different.

final and finally are keywords.finalize is a method.

final

is a keyword, which is used for below purpose
1. To declare constants.
2. To define final class.
3. To declare final methods.

finally

is a keyword, which is used in exception handling. finally block can exist only along with try and catch block. code enclosed in finally block gets executed irrespective of exception occurring or not.

finalize()

is method actually declared in Object class(which is base class of all classes in Java). This method can be overridden by any class, for garbage collection purpose.The java run time calls this method whenever it is about to recycle an object of the class