Various methods to print exception information in Java

In this article, we will discuss various methods provided by Java to print exception information in the console

As we discussed in the earlier article on runtime stack that, whenever exception is raised then respective method from where exception is raised is responsible for creating an exception object with following information like

  1. Name of the exception
  2. Description of the exception
  3. Location at which exception is raised i.e.; stack trace

 

Let us see various methods available from Throwable class to get/print exception information in the console

Method Description Format
printStackTrace(); Prints all details related to exception from exception object created by method Name-of-ex : Description-of-ex
At location (StackTrace)
toString(); Returns name & description of the exception in String format Name-of-ex : Description-of-ex
getMessage(); Returns detailed description of the exception thrown Description-of-ex
getCause(); Returns the cause of the exception;
Otherwise, returns null
Caused By: classname & stack trace
  • When exception are caught in the catch-block along with its exception object,
  • then we can use variable of exception object to invoke any of the above methods to print exception information in the console

 

Exception information for Arithmetic Exception:

Example 1: exception information using printStackTrace(); method

ExceptionInfoUsingPrintStackTraceMethod.java

package in.bench.resources.exception.handling;

public class ExceptionInfoUsingPrintStackTraceMethod {

	public static void main(String[] args) {

		try {
			// code which might raise exception

			int result = 18/0;
			System.out.println("Result of division : "
					+ result);
		}
		catch(ArithmeticException aex) {
			// corresponding handling code, 
			// if any exception from try block
			aex.printStackTrace();
		}
		finally {
			// finally block always gets executed 
			// for code clean-up activities

			System.out.println("finally block"
					+ " always gets executed");
			// rest of the code clean-up
		}
	}
}

Output:

java.lang.ArithmeticException: / by zero
	at in.bench.resources.exception.handling
	.ExceptionInfoUsingPrintStackTraceMethod.main(
			ExceptionInfoUsingPrintStackTraceMethod.java:11)
finally block always gets executed

 

Example 2: exception information using toString(); method

ExceptionInfoUsingToStringMethod.java

package in.bench.resources.exception.handling;

public class ExceptionInfoUsingToStringMethod {

	public static void main(String[] args) {

		try {
			// code which might raise exception

			int result = 18/0;
			System.out.println("Result of division : " 
					+ result);
		}
		catch(ArithmeticException aex) {
			// corresponding handling code, 
			// if any exception from try block
			System.out.println(aex.toString());
		}
		finally {
			// finally block always gets executed 
			// for code clean-up activities

			System.out.println("finally block"
					+ " always gets executed");
			// rest of the code clean-up
		}
	}
}

Output:

java.lang.ArithmeticException: / by zero
finally block always gets executed

 

Example 3: exception information using getMessage(); method

ExceptionInfoUsingGetMessageMethod.java

package in.bench.resources.exception.handling;

public class ExceptionInfoUsingGetMessageMethod {

	public static void main(String[] args) {

		try {
			// code which might raise exception

			int result = 18/0;
			System.out.println("Result of division : "
					+ result);
		}
		catch(ArithmeticException aex) {
			// corresponding handling code, 
			// if any exception from try block
			System.out.println(aex.getMessage());
		}
		finally {
			// finally block always gets executed 
			// for code clean-up activities

			System.out.println("finally block"
					+ " always gets executed");
			// rest of the code clean-up
		}
	}
}

Output:

/ by zero
finally block always gets executed

 

Example 4: cause of exception using getCause(); method

ExceptionInfoUsingGegtCauseMethod.java

package in.bench.resources.exception.handling;

public class ExceptionInfoUsingGegtCauseMethod {

	public static void main(String[] args) {

		try {

			// code which might raise exception

			int result = 18/0;
			System.out.println("Result of division : "
					+ result);
		}
		catch(ArithmeticException aex) {

			// corresponding handling code, 
			// if any exception from try block

			System.out.println(aex.getCause());
		}
		finally {

			// finally block always gets executed 
			// for code clean-up activities

			System.out.println("finally block"
					+ " always gets executed");
			// rest of the code clean-up
		}
	}
}

Output:

null
finally block always gets executed

 

References:

 

Read Also:

 

Happy Coding !!
Happy Learning !!

try with multiple catch-block in Java
finally block in Java