In Java we can use following methods to print exception information. Throwable class in Java defines all these methods to print exception informatio.
Method | Print Format |
---|---|
printStackTrace() | Name of exception: Description stack trace |
toString() | Name of exception: Description |
getMessage() | Description |
public class ExceptionHandling {
public static void main(String[] args) {
try {
System.out.println(10 / 0);
}
catch (Exception e){
e.printStackTrace();
System.out.println(e.toString());
System.out.println(e.getMessage());
}
}
}
Similar Java Tutorials
- Odd Even in Java
- Fibonacci Series Java
- Switch statements in Java
- For loop in Java
- While loop in Java
- Do while loop in Java
- Import statement in Java
- Abstract modifier in Java
- Strictfp modifier in Java
- Final variable in Java
- Adapter Class in Java
- Method signature in Java
- Method overloading in Java
- Has A Relationship
- Inheritance in Java
- Encapsulation in Java
- Abstraction in Java
- Data Hiding in Java
- Interface vs abstract class
- Method Overriding in Java
- Method Overloading in Java
- Coupling in Java
22 thoughts on “Methods to Print Exception Information in Java”