Java Atılabilir printStackTrace() yöntemi
Java Throwble sınıfının printStackTrace() yöntemi, istisnanın oluştuğu sınıf adı ve satır numarası gibi diğer ayrıntılarla birlikte Throwable'ı yazdırmak için kullanılır.
Sözdizimi
public void printStackTrace()
Geri dönmek
örnek 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()); } } } Şimdi Test Edin Çıktı:
java.lang.ArithmeticException: / by zero at ThrowablePrintStackTrace.main(ThrowablePrintStackTrace.java:5) Cause : null
Örnek 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!'); } } Şimdi Test Edin Çıktı:
Inside exceptionTest() method java.lang.Exception: Throwing localized message! at ThrowablePrintStackTraceExample2.exceptionTest(ThrowablePrintStackTraceExample2.java:11) at ThrowablePrintStackTraceExample2.main(ThrowablePrintStackTraceExample2.java:4)