Stack Tracing
in Java |
Prof. David Bernstein |
Computer Science Department |
bernstdh@jmu.edu |
StackTraceElement
:
StackFrameElement
objects are organized into a
last-in, first-out (LIFO) "collection" (called a stack)StackFrameElement
object (except,
perhaps, the top-most) represents
a method invocationStackFrameElement
object can
represent an execution point (e.g., the point at which
a Throwable
was created)main()
), the program terminates
and a message is printedStackFrameElement
objectsgetStackTrace()
method in the
Throwable
class will return the stack trace
(i.e., the LIFO StackTraceElement[]
) that was
created when the Throwable
was created
getStackTrace()
method belonging to
a Thread
object returns its current stack trace
and the current Thread
(loosely, the current
sequence of instructions) can be obtained
using the static currentThread()
method in the
Thread
classgetClassName()
getFileName()
getLineNumber()
getMethodName()
/** * A simple "bank" Account * * @version 1.0 * @author Prof. David Bernstein, James Madison University */ public class Account { protected double balance; protected int id; /** * Default constructor */ public Account() { balance = 0; id = -1; } /** * Explicit Value Constructor * * @param idNumber The ID for the Account * @param initialDeposit The initial deposit */ public Account(int idNumber, double initialDeposit) { balance = 0; id = idNumber; if (initialDeposit > 0) balance = initialDeposit; } /** * Determine the maximum amount that can be withdrawn * * @return The size of the maximum possible withdrawal */ public double amountAvailable() { return balance; } /** * Deposit money in this Account * * @param amount The size of the deposit * * @return false if unable to deposit and true otherwise */ public boolean deposit(double amount) { boolean ok; ok = false; if (amount >= 0) { balance += amount; ok = true; } return ok; } /** * Obtain the ID of this Account * * @return The ID of this Account */ public int getID() { return id; } /** * Withdraw money from this Account * * @param amount The size of the withdrawal * * @return false if unable to withdraw and true otherwise */ public boolean withdraw(double amount) { boolean ok; ok = false; if (amount >= 0) { // Check if available funds are sufficient // // Note: amountAvailble() is overriden in the // derived class OverdraftAccount. // Which version of amountAvailable() will be // called? // if (amountAvailable() >= amount) { balance -= amount; ok = true; } } return ok; } }
/** * A "bank" Account that allows the owner to withdraw more money * than is in the account * * @version 1.0 * @author Prof. David Bernstein, James Madison University */ public class OverdraftAccount extends Account { protected double overdraftLimit; /** * Constructor * * @param idNumber The ID for the Account * @param initialDeposit The initial deposit * @param limit The overdraft limit */ public OverdraftAccount(int idNumber, double initialDeposit, double limit) { balance = 0.0; id = idNumber; if (initialDeposit > 0) balance = initialDeposit; overdraftLimit = 0.0; if (limit > 0.0) overdraftLimit = limit; } /** * Determine the maximum amount that can be withdrawn * * Note: This method overrides amountAvailable() in Account. * This method is called by the method withdraw() which * is defined in Account. * * @return The size of the maximum possible withdrawal */ public double amountAvailable() { return (balance+overdraftLimit); } }
// Suppose we add the following to the amountAvailable() method // in OverdraftAccount StackTraceElement[] trace; trace = Thread.currentThread().getStackTrace(); for (int i=0; i<trace.length; i++) { System.out.println(trace[i]); } // And then do the following in the driver OverdraftAccount billSmith; billSmith = new OverdraftAccount(1001,15000.00,5000.00); billSmith.withdraw(19000.00);
The Stack Trace:
OverdraftAccount.amountAvailable(OverdraftAccount.java:20) Account.withdraw(Account.java:59) Driver.main(Driver.java:36)