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?

How to create your own Exception class

While developing applications in Java, you would often need to create your own Exceptions based on the need, along with built in Exception classes like ClassNotFoundException, NumberFormatException, ArithmeticException, ArrayIndexOutofBoundsException, etc… You can also create your own Exceptions, TestException

Below is an example to create an Exception class,

//Create our own Exception
class TestException extends Exception
{
	public String toString()
	{
		return "This is only Test Exception";
	}
}

public class MyOwnExceptionDemo {
	public static void main(String[] args) {
	try
	{
		throw new TestException();
	}
	catch(TestException te)
	{
		te.printStackTrace();

		//How to get class name from an object
		System.out.println("Exception class name:"+te.getClass().getName());
		System.out.println(te.toString());
	}
	}

}

You may also like to read:
what is printStackTrace() method
Output printStackTrace() to a String
Purpose of synchronized keyword

purpose of printStackTrace

printStackTrace() in Exception class, displays stack of details, from where(File name, class name and line number) the exception has been thrown, and further the methods/classes it has been propagated, and where it has been final caught. Click here to read output of printStackTrace into a String

Below is an example on how to use printStackTrace()

class CException
{
	public void testC() throws ArithmeticException,Exception
	{
		DException de = new DException();
		de.testD();
	}
}

class DException
{
	public void testD() throws ArithmeticException,Exception
	{
		int i,j=25,k=5;
			System.out.println("stmt 1");

			i = j/k;

			if(true)
			{
				throw new Exception("SampleException");
			}
			System.out.println("stmt 2");

	}
}

public class SimpleExcp {

	public static void main(String[] args){
		try
		{
			new CException().testC();

			try //nested try block
			{
				throw new Exception("TestingException");
			}
			catch(Exception te)
			{
				te.printStackTrace();
			}
		}
		catch(ArithmeticException ae)
		{
			System.out.println("--------AExcp:"+ae.getMessage());
			ae.printStackTrace();
		}
		catch(Exception et)
		{
			System.out.println("--------Excp:"+et.getMessage());
			et.printStackTrace();
		}
		finally
		{
			System.out.println("in finally block");
		}


	}

}


_______________________________________________________
Output:
——–Excp:SampleException
in finally block
java.lang.Exception: SampleException
at DException.testD(SimpleExcp.java:21)
at CException.testC(SimpleExcp.java:6)
at SimpleExcp.main(SimpleExcp.java:33)