Exceptions cs1043. Program Exceptions When a program detects an error, what should it do? – Nothing, simply allow the program to fail. – Implement a course.

Slides:



Advertisements
Similar presentations
Lilian Blot VARIABLE SCOPE EXCEPTIONS FINAL WORD Final Lecture Spring 2014 TPOP 1.
Advertisements

Topics Introduction Types of Errors Exceptions Exception Handling
Detecting Bugs Using Assertions Ben Scribner. Defining the Problem  Bugs exist  Unexpected errors happen Hardware failures Loss of data Data may exist.
Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
Chapter 16 Exception Handling. What is Exception Handling? A method of handling errors that informs the user of the problem and prevents the program from.
Yoshi
Exception Handling – illustrated by Java mMIC-SFT November 2003 Anders P. Ravn Aalborg University.
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.
COMP 121 Week 5: Exceptions and Exception Handling.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
Objectives In this chapter you will: Learn what an exception is Learn how to handle exceptions within a program See how a try / catch block is used to.
Chapter 12 By Tony Gaddis Modified by Elizabeth Adams Copyright © 2005 Pearson Addison-Wesley. All rights reserved.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Debugging Techniques1. 2 Introduction Bugs How to debug Using of debugger provided by the IDE Exception Handling Techniques.
COP INTERMEDIATE JAVA Exception Handling Serialization.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 16: Dec 6-10, 2010 Aditya Mathur Department of Computer Science Purdue.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
Exceptions Three categories of errors: Syntax errors Runtime errors Logic errors Syntax errors: rules of the language have not been followed. Runtime error:
Exceptions Used to signal errors or unexpected situations to calling code Should not be used for problems that can be dealt with reasonably within local.
Chapter 8 Exceptions. Topics Errors and Exceptions try-catch throwing Exceptions Exception propagation Assertions.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Applets & Applications CSC 171 FALL 2001 LECTURE 15.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Chapter 8  Errors and Exceptions Throwable class.
1 Exception Handling  Introduction to Exceptions  How exceptions are generated  A partial hierarchy of Java exceptions  Checked and Unchecked Exceptions.
Java Review 2 – Errors, Exceptions, Debugging Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 11: Handling Exceptions and Events J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Fourth.
Advanced Java Course Exception Handling. Throwables Class Throwable has two subclasses: –Error So bad that you never even think about trying to catch.
12.1 Exceptions The limitations of traditional methods of exception handling Error conditions are a certainty in programming Programmers make.
June 14, 2001Exception Handling in Java1 Richard S. Huntrods June 14, 2001 University of Calgary.
CIS 270—Application Development II Chapter 13—Exception Handling.
Handling ErrorstMyn1 Handling Errors Up to this point we haven't worried much about errors or exceptions. First, let's distinguish between errors and exceptions.
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
COMPUTER PROGRAMMING 2 Exceptions. What are Exceptions? Unexpected events that happen when the code is executing (during runtime). Exceptions are types.
Slides Credit Umair Javed LUMS Web Application Development.
COMP Exception Handling Yi Hong June 10, 2015.
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.
Controlling Execution Programming Right from the Start with Visual Basic.NET 1/e 8.
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
Exception Handling Programmers must deal with errors and exceptional situations: User input errors Device errors Empty disk space, no memory Component.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
1 An Exception is… An unusual, often unpredictable event, detectable by software or hardware, that requires special processing An exception handler is.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Exceptions Handling Exceptions with try and catch The finally-block The throws.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
Error Handling Tonga Institute of Higher Education.
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Introduction to Exceptions in Java CS201, SW Development Methods.
Eighth Lecture Exception Handling in Java
Java Exceptions a quick review….
Exception Handling and Event Handling
Chapter 14: Exception Handling
Exception Handling and Reading / Writing Files
Exception Handling.
Errors and Exceptions Error Errors are the wrongs that can make a program to go wrong. An error may produce an incorrect output or may terminate the execution.
Tenth step for Learning C++ Programming
Java Basics Exception Handling.
Exception Handling.
Presentation transcript:

Exceptions cs1043

Program Exceptions When a program detects an error, what should it do? – Nothing, simply allow the program to fail. – Implement a course of action to correct the error.

Example: A user enters an incorrect URL into a web browser. – Should the browser crash? – Should the browser request another URL?

Exception Handling Software is designed to detect and correct most failures. Detection and correction of an error is known as exception handling.

Java Exceptions The Java compiler places exceptions into two categories: – Checked exceptions – Unchecked exceptions

Checked Exceptions The compiler requires the programmer to either: 1.write a competent exception handler (CEH) for any checked exceptions. - Example: Input-output operations (reading and writing files and data streams). 2.Throw the exception hoping another method in the calling-tree will be able to handle the exception. This require the throws keyword.

Unchecked Exceptions – Example double z = x / y; The compiler does not require any special action to prevent the division when y=0.

Unchecked Exceptions The programmer can choose to: 1.Ignore the exception and hope for the best. 2.Catch the exception and implement an action to recover from the exception. Note: if the program cannot recover from an exception, the program exits with a fatal exception runtime error.

Unchecked Exception Example double z; if ( y != 0 ) //prevent 0-division z = x / y; else // perform some other action

Checked Exception Two choices for the programmer: 1.Throw the exception to the referencing method and hope for recovery. 2.Catch the exception with a competent exception handler

Java Keywords for Exception Handling An unchecked exception could use the keyword “throw”. Checked exceptions can use any of these: – throws – throw – try, catch, & finally

The Competent Exception Handler The competent exception handler uses a combination of these three keywords: try, catch, and finally. If the programmer wishes to send the exception to the referencing method, then use the throws keyword.

Syntax for CEH In order: 1.try-block – one per CEH 2.catch-blocks - zero or more per CEH 3.finally-block – zero or one per CEH

1. CEH try-Block A CEH must have a try-block The try-block consists of the try keyword followed by a body of code.

2. CEH catch-Blocks The try-block is followed by zero or more catch blocks. – Each catch block will perform an action based on the exception type

3. CEH finally-Block The finally-block is required if there are zero catch blocks. The finally-block is optional if there is at least one catch-block.

CEH The CEH can be nested just like other control structures.

Unchecked Exception with “CEH”

User Defined Exception Class