Home > JAVA > Call Stack & Stack Trace

Call Stack & Stack Trace

Call Stack adalah urutan pemanggilam method di dalam Java. Pertama kali tentu saja method main. Kemudian dari method main ini memanggil method lain lalu method lain tersebut memanggil method yang lain lagi, begitu seterusnya sehingga pemanggilan methodnya bertumpuk.


package com.wordpress.itfromzerotohero;

public class CallStack {

public static void main(String[] args) {
 System.out.println("main method in action");
 methodSatu();
 }

 static void methodSatu(){
 System.out.println("methodSatu in action");
 methodDua();
 }

 static void methodDua(){
 System.out.println("methodDua in action");
 }

 static void methodTiga(){
 System.out.println("methodTiga in action");
 }
}

Hasil

main methid in action
methodSatu in action
methodDua in action

 

Stack Trace adalah jejak call stack ketika exception muncul. Kita dapat melihat urutan pemanggilan method mulai dari method yang menggenerate exception.


package com.wordpress.itfromzerotohero;

public class StackTrace {

public static void main(String[] args) {
 System.out.println("main method in action");
 methodSatu();
 System.out.println("bagian ini tidak akan dieksekusi");
 }

 static void methodSatu(){
 System.out.println("methodSatu in action");
 methodDua();
 }

 static void methodDua(){
 System.out.println("methodDua in action");
 methodTiga();
 }

 static void methodTiga(){
 System.out.println("methodTiga in action");
 String a = null;
 System.out.println(a.length());
 }

}

Hasilnya

Exception in thread “main” main method in action
methodSatu in action
methodDua in action
methodTiga in action
java.lang.NullPointerException
at com.wordpress.itfromzerotohero.StackTrace.methodTiga(StackTrace.java:24)
at com.wordpress.itfromzerotohero.StackTrace.methodDua(StackTrace.java:18)
at com.wordpress.itfromzerotohero.StackTrace.methodSatu(StackTrace.java:13)
at com.wordpress.itfromzerotohero.StackTrace.main(StackTrace.java:7)

Stack trace dimulai dari bagian ‘Exception in thread “main”‘ bisa dilihat bahwa pada methodTiga terjadi exception, setelah itu diperlihatkan urutan pemanggilan method hingga method main.

Categories: JAVA
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: