How can I print the stack?
package de.codegym.task.task09.task0917;
/*
Ungeprüfte Ausnahmen abfangen
*/
public class Solution {
public static void main(String[] args) {
ausnahmenBehandeln(new Solution());
}
public static void ausnahmenBehandeln(Solution obj) {
try{
obj.methode1();
obj.methode2();
obj.methode3();
}
catch(NullPointerException e)
{
System.out.println("Error: " + e.getMessage());
e.printStackTrace();
}
catch(IndexOutOfBoundsException e)
{
System.out.println("Error: " + e.getMessage());
e.printStackTrace();
}
catch(NumberFormatException e)
{
System.out.println("Error: " + e.getMessage());
e.printStackTrace();
}
}
public static void stackAusdrucken(Throwable throwable) {
System.out.println(throwable);
for (StackTraceElement element : throwable.getStackTrace()) {
System.out.println(element);
}
}
public void methode1() {
throw new NullPointerException();
}
public void methode2() {
throw new IndexOutOfBoundsException();
}
public void methode3() {
throw new NumberFormatException();
}
}