Chapter 8 Errors and Exceptions.

Slides:



Advertisements
Similar presentations
Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
Advertisements

Yoshi
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.
An Introduction to Java Programming and Object- Oriented Application Development Chapter 8 Exceptions and Assertions.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
© 2004 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 : Exceptions Intermediate Java Programming Summer 2007.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 13 Exception Handling.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions.
Chapter 8Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 8 l Basic Exception Handling »the mechanics of exceptions l.
Exceptions1 Syntax, semantics, and pragmatics. Exceptions2 Syntax, semantics, pragmatics Syntax –How it looks, i.e. how we have to program to satisfy.
Exception Handling 1 CISC6795, Spring Introduction 2 Exception – an indication of a problem that occurs during a program’s execution, for examples:
Error Handling with Exceptions Concepts C and other earlier languages often had multiple error-handling schemes, and these were generally established.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
COP INTERMEDIATE JAVA Exception Handling Serialization.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Chapter 8  Errors and Exceptions Throwable class.
Handling Errors with Exception (in Java) Project 10 CSC 420.
Introduction to Java Chapter 11 Error Handling. Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle.
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.
Java Software Solutions Foundations of Program Design Sixth Edition
Preventing and Correcting Errors
1 Exception Handling Introduction to Exception Handling Exception Handling in PLs –Ada –C++ –Java Sebesta Chapter 14.
Exception Handling. Exceptions and Errors When a problem encounters and unexpected termination or fault, it is called an exception When we try and divide.
June 14, 2001Exception Handling in Java1 Richard S. Huntrods June 14, 2001 University of Calgary.
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
CIS 270—Application Development II Chapter 13—Exception Handling.
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
Slides Credit Umair Javed LUMS Web Application Development.
Defensive Programming 1 Nikolaus Embgen. Topics 1.Motivation 2.The concept 3.What can we do? 4.How to use this? 5.What else can we do? 6.The conclusion.
Handling Exceptions in java. Exception handling blocks try { body-code } catch (exception-classname variable-name) { handler-code }
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Java Software Solutions Lewis and Loftus Chapter 14 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Advanced Flow of Control --
COMP Exception Handling Yi Hong June 10, 2015.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
Exception Handling Unit-6. Introduction An exception is a problem that arises during the execution of a program. An exception can occur for many different.
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.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
Exceptions cs1043. Program Exceptions When a program detects an error, what should it do? – Nothing, simply allow the program to fail. – Implement a course.
COP4020 Programming Languages Exception Handling Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
Computer Programming with JAVA Chapter 8. Exception Handling Basic Exception Handling the mechanics of exceptions Defining and Using Exceptions some "simple"
Compilers and Interpreters. HARDWARE Machine LanguageAssembly Language High Level Language C++ Visual Basic JAVA Humans.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
Compilers and Interpreters
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
Exceptions in OO Programming Introduction Errors Exceptions in Java Handling exceptions The Try-Catch-Finally mechanism Example code Exception propagation.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
1 Handling Errors and Exceptions Chapter 6. 2 Objectives You will be able to: 1. Use the try, catch, and finally statements to handle exceptions. 2. Raise.
Object Throwable ErrorException RuntimeException.
Eighth Lecture Exception Handling in Java
C++ Exceptions.
Generics, Exceptions and Undo Command
Tirgul 13 Exceptions 1.
Java Programming Language
Chapter 13 Exception Handling
Introduction Exception handling Exception Handles errors
Chapter 14: Exception Handling
Partnership.
Chapter 12 Exception Handling
Exception Handling and Reading / Writing Files
Web Design & Development Lecture 7
Chapter 13 Exception Handling
Exception Handling and Event Handling
Exception Handling.
Presentation transcript:

Chapter 8 Errors and Exceptions

Overview Handling errors Preventing resource leaks. With return values. With setjmp/longjmp. With exceptions. Preventing resource leaks. Logging and debugging.

Handling errors with return codes. Simple idea but: Makes it easy to ignore errors The code becomes harder to read, write and understand. There is no universal convention for communicating error information.

An example of error checking

Useful C Functions for handling errors errno perror strerror error and friends err/warn setjmp/longjmp

Exceptions in C++ Mechanism: try{....}catch(....){...}catch(...){...}... First sequence of code is code to execute. This code can “throw” exceptions which are “caught” by the apprpriate catch phrase.

Managing exceptions Exceptions in java vs C++ (checked or unchecked)‏ Not catching an exception will cause a program crash, -> DOS attack. At least, catch everything at top level to avoid “spilling beans”. Deeper down, catch only what you can handle. Watch for “finally” clauses in Microsoft C++/Java (pp 273/274)‏

Preventing Resource Leaks Only real security risk is DOS, but can cause serious performance problems. Very hard to track down and identify. Usually manifest themselves only in production Very hard to trace back to their origin. Usually due to seldom traversed instruction paths, like error or exception handlers.

Watch out for multiple returns

Logging and Debugging Centralize the output operation. There are packages for this. Provide a uniform view. Make it easier to change medium, machine, etc. Provide time stamps. Log every important action, including failures! Protect the logs.

A few final words Keep debugging aids out of production Keep back-door access code out of production Clean out Backup files Say “NO” to Easter Eggs.

Resource leaks “gotchas” Watch for multiple return statements In C++, classes can be used to advantage, but watch out for strange modifications.