Exception Handling An Exception is an indication of a problem that occurs during a program’s execution. Exception handling enables the programmer to create.

Slides:



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

Error-handling using exceptions
Topics Introduction Types of Errors Exceptions Exception Handling
Error Handling in.NET Exceptions. Error Handling Old way (Win32 API and COM): MyFunction() { error_1 = doSomething(); if (error_1) display error else.
Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer.
CSE 1302 Lecture 21 Exception Handling and Parallel Programming Richard Gesick.
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.
Errors and Exceptions The objectives of this chapter are: To understand the exception handling mechanism defined in Java To explain the difference between.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Try…Catch…Finally Blocks ( continued ) Generic catch clause –Omit argument list with the catch –Any exception thrown is handled by executing code within.
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.
 Both System.out and System.err are streams—a sequence of bytes.  System.out (the standard output stream) displays output  System.err (the standard.
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
Mahmoud Rafeek Alfarra Computer Programming || Chapter 2: Exception handling.
Exception Handling 1 CISC6795, Spring Introduction 2 Exception – an indication of a problem that occurs during a program’s execution, for examples:
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Exception Handling: A Deeper.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Exceptions Briana B. Morrison CSE 1302C Spring 2010.
Understand Error Handling Software Development Fundamentals LESSON 1.4.
E XCEPTION H ANDLING Chapter 11 C S 442: A DVANCED J AVA P ROGRAMMING.
Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling.
Chapter 15 Strings String::Concat String::CompareTo, Equals, == If( string1 == S”Hello”) String1->Equals(S”Hello”) String1->CompareTo(S”Hello”) CompareTo.
Exceptions. 2 Objectives Introduce C# exception handling –library exception types –custom exceptions Describe keywords used for exception handling –try.
Java Review 2 – Errors, Exceptions, Debugging Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 12: Advanced Topics: Exception Handling Visual Basic.NET Programming: From Problem Analysis to Program Design.
 2006 Pearson Education, Inc. All rights reserved Exception Handling.
 2009 Pearson Education, Inc. All rights reserved Exception Handling.
 2006 Pearson Education, Inc. All rights reserved Exception Handling.
Dr. Abraham. Exception Any problem that VB or OS could not handle Robust program A program that performs well not only under ordinary conditions but also.
 2002 Prentice Hall. All rights reserved. 1 Exception Handling Overview Exception handling –Enable clear, robust and more fault-tolerant programs –Process.
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
COMPUTER PROGRAMMING 2 Exceptions. What are Exceptions? Unexpected events that happen when the code is executing (during runtime). Exceptions are types.
Understanding Events and Exceptions Lesson 3. Objective Domain Matrix Skills/ConceptsMTA Exam Objectives Understand events and event handling Understand.
Pemrograman VisualMinggu …12… Page 1 MINGGU Ke Duabelas Pemrograman Visual Pokok Bahasan: Exception Handling Tujuan Instruksional Khusus: Mahasiswa dapat.
Exceptions Programming in C# Exceptions CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
VB.Net - Exceptions Copyright © Martin Schray
Introduction to Exception Handling and Defensive Programming.
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.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
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.
Exception Handling in C++. Outline What exceptions are and when to use them Using try, catch and throw to detect, handle and indicate exceptions, respectively.
CSCI 383 Object-Oriented Programming & Design Lecture 20 Martin van Bommel.
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
Exception Handling How to handle the runtime errors.
C# Exceptions 1 CNS 3260 C#.NET Software Development.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Lecture 18B Exception Handling and Richard Gesick.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Enhanced Car Payment Calculator Application Introducing Exception Handling.
Lecture 11 Dr. Eng. Ibrahim El-Nahry Exception Handling.
Chapter 11 – Exception Handling
Exception Handling in C++
16 Exception Handling.
Exceptions C++ Interlude 3
Exception Handling and
CNS 3260 C# .NET Software Development
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 Chapter 9 Edited by JJ.
Part B – Structured Exception Handling
Exception Handling By: Enas Naffar.
Programming in C# Lesson 5. Exceptions..
Programming in C# CHAPTER - 7
Presentation transcript:

Exception Handling An Exception is an indication of a problem that occurs during a program’s execution. Exception handling enables the programmer to create applications that can resolve (or handle) the exception. In many cases, handling an exception allows a program to notify the user of the problem, then terminate in a controlled manner Exception handling is designed to process synchronous errors-errors that occur during the normal program flow of control.

Common example of this type of errors Out-of-range array Divide by zero Arithmetic overflow – a value outside the representable range of value Invalid method parameter I/O completion File not found, etc.

Exception handling is geared to situations in which the method or function that detects an error is unable to handle it Such a method or function throws an exception There is no guarantee that there will be an exception handler-code that executes when the program detects an exception – to process that kind of exception. If there is, the exception will caught and handled.

Throw an exception When a method or function called in program detects an exception or when the CLR( Common Language Runtime) detects a problem, the method/function or CLR throw an exception The point in the program at which an exception occurs is called the throw point-an important location for debugging purpose Exceptions are objects of classes that extend class Exception of namespace System

MC++ Exception handling MC++ uses try block to enable exception handling. A try block consists of keyword try and { } that defines a block of code in which exceptions may occur. It encloses Statement that might cause exceptions Statements that should not execute if an exception occurs. Immediately following the try block are zero or more catch blocks (also called catch handlers) try {… } catch ( Exception * ) { … }

Catch handler Each catch handler specifies in parentheses an exception parameter that represent the type of exception the catch handler can handle. If it includes an optional parameter name, catch handler can use it to interact with a caught exception object. Optionally, a parameterless catch handler catches all exception types. Optional __finally block contains code that always executes, regardless of whether an exception occurs.

A try block must be followed by at least one catch block or __finally block If no exception occur in a try block, the CLR ignores the exception handlers for that block Exception properties Message – stores the error message associated with an Exception object. This message may be a default message associated with the exception type or a customized message passed to an exception object’s constructor when the exception object is constructed.

Exception properties StackTrace – contains a string that represents the method call stack The runtime environment keeps a list of method calls that have been made up to a given moment. StackTrace string represents this list of method that had not finished processing at the time the exception occurred. The exact location at which the exception occurs in the program is called the exception’s throw point

Example: DivideByZeroException try { int res = num/de; } catch(DivideByZeroException *e) { Console::WriteLine(e->Message);

Example- FormatException try{ Cosole::Write(S”Enter an interger”); int x = Convert::ToInt32(Console::ReadLine()); } catch(FormatException *) { Console::WriteLine(S”You must enter integer”);

.NET exception hierarchy Class Exception of namespace System is the base class of the ,NET Framework exception hierarchy Two of the most important derived classes of exception are ApplicationException and SystemException. ApplicationException is a base class that programmers can extend to create exception data types that are specific to their application The CLR can generate SystemException at any point during the execution of the program. Many of these exception can be avoided by coding properly. These are called runtime exception and they derive from class SystemException. For example, IndexOutOfRangeException NullReferenceException FileNotFoundException For a complete list of derived classes of Exception, look up Visual Studio .NET document