logo

Metodo printStackTrace() Java Throwable

Il metodo printStackTrace() della classe Java Throwble viene utilizzato per stampare Throwable insieme ad altri dettagli come il nome della classe e il numero di riga in cui si è verificata l'eccezione.

Sintassi

 public void printStackTrace() 

Ritorno

Esempio 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()); } } } 
Provalo adesso

Produzione:

 java.lang.ArithmeticException: / by zero at ThrowablePrintStackTrace.main(ThrowablePrintStackTrace.java:5) Cause : null 

Esempio 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!'); } } 
Provalo adesso

Produzione:

industria e fabbrica
 Inside exceptionTest() method java.lang.Exception: Throwing localized message! at ThrowablePrintStackTraceExample2.exceptionTest(ThrowablePrintStackTraceExample2.java:11) at ThrowablePrintStackTraceExample2.main(ThrowablePrintStackTraceExample2.java:4)