Java – Various methods to print exception information

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

1. Methods to print Exception information:

MethodDescriptionFormat
printStackTrace();Prints all details related to exception from exception object created by methodName-of-ex : Description-of-ex
At location (StackTrace)
toString();Returns name & description of the exception in String formatName-of-ex : Description-of-ex
getMessage();Returns detailed description of the exception thrownDescription-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

2. Exception information for Arithmetic Exception:

  1. Exception information using printStackTrace(); method
  2. Exception information using toString(); method
  3. Exception information using getMessage(); method
  4. Cause of Exception using getCause(); method

2.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

2.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

2.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

2.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

Related Articles:

References:

Happy Coding !!
Happy Learning !!

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