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
- Name of the exception
- Description of the exception
- 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:
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
2. Exception information for Arithmetic Exception:
- Exception information using printStackTrace(); method
- Exception information using toString(); method
- Exception information using getMessage(); method
- 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:
- Java – Exception Handling
- Java – Exception Hierarchy
- Java – 5 important keywords in Java Exception handling
- Java – Runtime mechanism, what happens when exception is thrown ?
- Java – Checked Exception v/s Unchecked Exception
- Java – Exception propagation
- Java – try-catch block
- Java – finally block
- Java – try with multiple catch blocks
- Java – Nested try-catch block
- Java – Returning value from method having try-catch-finally blocks
- Java – return statement with finally block
- Java – final v/s finally v/s finalize
- Java – Various methods to print exception information
- Java – throw keyword
- Java – throws keyword
- Java – throw v/s throws
- Java – Difference between throws clause and try-catch-finally block
- Java – Rules for Exception handling w.r.t Method Overriding
- Java – User-defined or Custom exception
- Java – Difference between ClassNotFoundException v/s NoClassDefFoundError
- Java – Top Exception and Error
- Java – Interview question and answers on Exception Handling
- Java 7 – try with resources
- Java 7 – multi-catch block
References:
- http://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html
- https://docs.oracle.com/javase/tutorial/essential/exceptions/
- https://docs.oracle.com/javase/7/docs/api/java/lang/Exception.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/Error.html
- https://docs.oracle.com/javase/specs/jls/se7/html/jls-11.html
- https://docs.oracle.com/javase/tutorial/essential/exceptions/try.html
- https://docs.oracle.com/javase/tutorial/essential/exceptions/catch.html
- https://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/ArithmeticException.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/NullPointerException.html
- http://docs.oracle.com/javase/7/docs/api/java/lang/ArrayIndexOutOfBoundsException.html
- http://www.oracle.com/technetwork/java/effective-exceptions-092345.html
- http://otfried.org/courses/cs206/slides/slides-stackframes.pdf
Happy Coding !!
Happy Learning !!