The Call Stack - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

The Call Stack

Description:

The call stack is stored in memory by the Java virtual machine ... Since local variables and parameters are stored on the stack, recursive calls work: ... – PowerPoint PPT presentation

Number of Views:365
Avg rating:3.0/5.0
Slides: 13
Provided by: aseR1
Category:
Tags: call | stack

less

Transcript and Presenter's Notes

Title: The Call Stack


1
The Call Stack
  • (Exceptions revisited)

2
The Call Stack
  • When method calls are made, the programming
    language must keep track of who called who
  • and where to jump back to when method is
    finished
  • This is done with the call stack
  • a list of the methods that have been called to
    get to the current code

3
The Call Stack
  • If a method privateMethod() is running now, the
    stack might look like this

privateMethod()
called
obj.someMethod()
called
doStuff()
called
main()
4
Returning Values
  • When methods return a value, it is passed back
    and the original call is removed

privateMethod()
returns
called
obj.someMethod()
returns
called
doStuff()
returns
called
main()
5
Storing
  • The call stack is stored in memory by the Java
    virtual machine
  • All of the methods parameters and local
    variables and parameters are part of its entry on
    the stack
  • this is why recursive calls all run separately
    different calls and variables on the call stack

6
Recursion on the Stack
  • Since local variables and parameters are stored
    on the stack, recursive calls work

binSearch(2,4)
called
Each has its own local variables
binSearch(0,4)
called
binSearch(0,8)
called
main()
7
Exceptions and the Stack
  • When a method throws an exception, that gets
    passed (instead of return value)

throw new SomeException()
privateMethod()
SomeException
called
obj.someMethod()
SomeException
called
doStuff()
SomeException
called
main()
program stops
8
Stack Display
  • When an exception halts the program, the output
    is a representation of the call stack
  • Exception in thread main SomeException
  • at MyClass.privateMethod(MyClass.java8)
  • at MyClass.someMethod(MyClass.java56)
  • at MainProg.doStuff(MainProg.java31)
  • at MainProg.main(MainProg.java127)
  • This stack trace corresponds to the previous
    example

9
Catching Exceptions
  • If an exception is caught, its propogation stops
    and the function returns as usual

throw new SomeException()
privateMethod()
SomeException
called
obj.someMethod()
SomeException
called
doStuff()
catch(SomeException e)
returns normally
called
main()
10
Designing with Exceptions
  • An exception should occur if some uncommon
    condition occurs that the function cant handle
  • shouldnt happen if all goes well
  • An exception should be used in exceptional
    circumstances where the function cant recover by
    itself
  • The function should throw an exception
  • throw new TypeOfException(error message)

11
Example
throw new NullPointerException()
name.length()
NullPointerException
called
student.getNameSize()
catch(Exception e)
return normally
called
doStuff() defines student with no name
returns normally
called
main()
12
Designing with Exceptions
  • Some function up the call stack should catch the
    exception catch(TypeOfException e)
  • whatever function can actually handle the problem
    should do so
  • e.g.
  • user input function throws InputMismatchException,
    menu function catches it and asks for another
    choice
  • network IO function cant read a webpage, main
    program pops up error message
Write a Comment
User Comments (0)
About PowerShow.com