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.

Slides:



Advertisements
Similar presentations
Pearson Education, Inc. All rights reserved. 1.. Exception Handling.
Advertisements

ABNIAC The following slide presentation is to acquaint the student with ABNIAC. The version used for presentation is the Java version, which can be found.
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
Exception Handling Chapter 15 2 What You Will Learn Use try, throw, catch to watch for indicate exceptions handle How to process exceptions and failures.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Exceptions and Exception Handling Carl Alphonce CSE116.
Exceptions and Exception Handling (2) Carl Alphonce CSE116.
11-Jun-15 Exceptions. 2 Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a.
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Ten debugging techniques. The execution process Execution proceeds in a standard series of steps Compute values of subexpressions first Then call value.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
Java Exceptions. Intro to Exceptions  What are exceptions? –Events that occur during the execution of a program that interrupt the normal flow of control.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Chapter 8  Errors and Exceptions Throwable class.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
1 - buttons Click “Step Forward” to execute one line of the program. Click “Reset” to start over. “Play,” “Stop,” and “Step Back” are disabled in this.
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
Preventing and Correcting Errors
June 14, 2001Exception Handling in Java1 Richard S. Huntrods June 14, 2001 University of Calgary.
1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about.
Object Oriented Programming
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
CIS 270—Application Development II Chapter 13—Exception Handling.
Chapter 12: Exception Handling
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 12 l Basics of Recursion l Programming with Recursion Recursion.
Handling Exceptions in java. Exception handling blocks try { body-code } catch (exception-classname variable-name) { handler-code }
ITEC 352 Lecture 18 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly.
Exceptions Handling Exceptionally Sticky Problems.
COMP Exception Handling Yi Hong June 10, 2015.
Exception Handling in JAVA. Introduction Exception is an abnormal condition that arises when executing a program. In the languages that do not support.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
Chapter 12. Recursion Basics of Recursion Programming with Recursion Computer Programming with JAVA.
ITEC 352 Lecture 19 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Stacks Function activation / deactivation.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Winter 2006CISC121 - Prof. McLeod1 Stuff No stuff today!
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
Chapter 8-Exception Handling/ Robust Programming.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Exception Handling How to handle the runtime errors.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Exception Handling. VB.NET has an inbuilt class that deals with errors. The Class is called Exception. When an exception error is found, an Exception.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Exceptions an unusual condition – e.g. division by zero – e.g. file doesn't exist – e.g. illegal type – etc. etc… typically a run-time error – i.e. during.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Introduction to Exceptions in Java CS201, SW Development Methods.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Object Throwable ErrorException RuntimeException.
Eighth Lecture Exception Handling in Java
Generics, Exceptions and Undo Command
User-Written Functions
Handling Exceptionally Sticky Problems
Chapter 9 Recursion.
CS102 – Exceptions David Davenport Latest: May 2015
Chapter 14: Exception Handling
Exceptions 10-Nov-18.
Exceptions with Functions
Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.
Exception Handling Oo28.
Exception Handling Imran Rashid CTO at ManiWeber Technologies.
Exceptions 25-Apr-19.
Exceptions 22-Apr-19.
Handling Exceptionally Sticky Problems
Exceptions 10-May-19.
Exceptions 5-Jul-19.
CMSC 202 Exceptions.
Throwing, Catching Defining
Exceptions Review Checked Vs. Unchecked Exceptions
Presentation transcript:

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() obj.someMethod() doStuff() main() called

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

5 Storing The call stack is stored in memory by the Java virtual machine All of the method’s 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) binSearch(0,4) binSearch(0,8) main() called Each has it’s own local variables

7 Exceptions and the Stack When a method throws an exception, that gets passed (instead of return value) privateMethod() obj.someMethod() doStuff() main() called SomeException throw new SomeException(); 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.java:8) at MyClass.someMethod(MyClass.java:56) at MainProg.doStuff(MainProg.java:31) at MainProg.main(MainProg.java:127) 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: privateMethod() obj.someMethod() doStuff() main() called SomeException throw new SomeException(); returns normally catch(SomeException e)

10 Designing with Exceptions An exception should occur if some uncommon condition occurs that the function can’t handle  shouldn’t happen if all goes well An exception should be used in “exceptional” circumstances where the function can’t recover by itself The function should throw an exception  throw new TypeOfException(“error message”);

11 Example name.length() student.getNameSize() doStuff() – defines student with no name main() called NullPointerException return normally throw new NullPointerException(); returns normally catch(Exception e)

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 can’t read a webpage, main program pops up error message