PrintStackTrace()-metoden i Java Throwble-klassen bruges til at udskrive Throwable sammen med andre detaljer som klassenavn og linjenummer, hvor undtagelsen opstod.
Syntaks
public void printStackTrace()
Vend tilbage
Eksempel 1
import java.lang.Throwable; public class ThrowablePrintStackTraceExample1 { public static void main(String[] args) throws Throwable { try{ int i=4/0; }catch(Throwable e){ e.printStackTrace(); System.err.println('Cause : '+e.getCause()); } } }Test det nu
Produktion:
java.lang.ArithmeticException: / by zero at ThrowablePrintStackTrace.main(ThrowablePrintStackTrace.java:5) Cause : null
Eksempel 2
public class ThrowablePrintStackTraceExample2 { public static void main(String[] args)throws Throwable { try{ exceptionTest(); }catch(Throwable t){ t.printStackTrace(); } } public static void exceptionTest() throws Exception{ System.out.println('Inside exceptionTest() method'); throw new Exception('Throwing localized message!'); } }Test det nu
Produktion:
hvordan man bruger mysql workbench
Inside exceptionTest() method java.lang.Exception: Throwing localized message! at ThrowablePrintStackTraceExample2.exceptionTest(ThrowablePrintStackTraceExample2.java:11) at ThrowablePrintStackTraceExample2.main(ThrowablePrintStackTraceExample2.java:4)