Lecture 11 Dr. Eng. Ibrahim El-Nahry Exception Handling.

Slides:



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

Topics Introduction Types of Errors Exceptions Exception Handling
Yoshi
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 2 nd Lecture Pavel Ježek
Lecture 9. 2 Exception  An exception is a unusual, often unpredictable event, detectable by software or hardware, that requires special processing occurring.
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.
Index Exception handling Exception In Java Exception Types
Try…Catch…Finally Blocks ( continued ) Generic catch clause –Omit argument list with the catch –Any exception thrown is handled by executing code within.
SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions.
Exception Handling 1 CISC6795, Spring Introduction 2 Exception – an indication of a problem that occurs during a program’s execution, for examples:
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
CS 3260 Dennis A. Fairclough Version 1.0
Exceptions Briana B. Morrison CSE 1302C Spring 2010.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.
E XCEPTION H ANDLING Chapter 11 C S 442: A DVANCED J AVA P ROGRAMMING.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 14 - Exception Handling Outline 14.1Introduction 14.2When Exception Handling Should Be Used 14.3Other.
Exception Handling An Exception is an indication of a problem that occurs during a program’s execution. Exception handling enables the programmer to create.
 2002 Prentice Hall. All rights reserved. 1 Chapter 11 – Exception Handling Outline 11.1 Introduction 11.2 Exception Handling Overview 11.3 Example: DivideByZeroException.
Exceptions. 2 Objectives Introduce C# exception handling –library exception types –custom exceptions Describe keywords used for exception handling –try.
 2009 Pearson Education, Inc. All rights reserved Exception Handling Many slides modified by Prof. L. Lilien (even many without explicit message).
 2006 Pearson Education, Inc. All rights reserved 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.
 2002 Prentice Hall. All rights reserved. 1 Chapter 11 – Exception Handling Outline 11.1 Introduction 11.2 Exception Handling Overview 11.3 Example: DivideByZeroException.
Preventing and Correcting Errors
Exception Handling. Exceptions and Errors When a problem encounters and unexpected termination or fault, it is called an exception When we try and divide.
Chapter 13 Exception Handling F Claiming Exceptions F Throwing Exceptions F Catching Exceptions F Rethrowing Exceptions  The finally Clause F Cautions.
 2002 Prentice Hall. All rights reserved. 1 Exception Handling Overview Exception handling –Enable clear, robust and more fault-tolerant programs –Process.
06 Exception Handling. 2 Contents What is an Exception? Exception-handling in Java Types of Exceptions Exception Hierarchy try-catch()-finally Statement.
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
Java Programming Exception Handling. The exception handling is one of the powerful mechanism provided in java. It provides the mechanism to handle the.
COMPUTER PROGRAMMING 2 Exceptions. What are Exceptions? Unexpected events that happen when the code is executing (during runtime). Exceptions are types.
Pemrograman VisualMinggu …12… Page 1 MINGGU Ke Duabelas Pemrograman Visual Pokok Bahasan: Exception Handling Tujuan Instruksional Khusus: Mahasiswa dapat.
VB.Net - Exceptions Copyright © Martin Schray
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 10.
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.
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.
Exceptions, handling exceptions & message boxes Year 11 Information Technology.
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.
Exceptions in Java. What is an exception? An exception is an error condition that changes the normal flow of control in a program Exceptions in Java separates.
Exception-Handling Fundamentals  A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 2 nd Lecture Pavel Ježek
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
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.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
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.
Agenda Introduction Errors and Exception Exception Hierarchy Classification of Exceptions Built in Exceptions Exception Handling in Java User defined.
Lecture 18B Exception Handling and Richard Gesick.
Chapter 11 – Exception Handling
C++ Exceptions.
16 Exception Handling.
Chapter 10 – Exception Handling
Tirgul 13 Exceptions 1.
Chapter 12 – Exception Handling
CNS 3260 C# .NET Software Development
ATS Application Programming: Java Programming
Chapter 12 Exception Handling
Part B – Structured Exception Handling
Programming in C# CHAPTER - 7
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.
Lecture 9.
C# Language & .NET Platform 4th Lecture
Presentation transcript:

Lecture 11 Dr. Eng. Ibrahim El-Nahry Exception Handling

2 Handling Exception If without handling, Program crashes Falls into unknown state An exception handler is a section of program code that is designed to execute when a particular exception occurs Resolve the exception Lead to known state, such as exiting the program

Handling Exception Create application that can handle or resolve exception Enable clear, robust and more fault-tolerant programs 3

Keywords Try Include codes in which exceptions might occur Catch Represent types of exceptions the catch can handle Finally (Optional) codes present here will always execute Exception handling Process synchronous errors Follows the termination model of exception handling 4

Catch type Must be of class Exception or one that extends it directly or indirectly Stack unwinding CLR attempt to locate an enclosing try block in a calling method 5

The.Net Framework 6

System.Exception Properties e.Message the error message as a string; set in new Exception(msg); e.StackTrace trace of the method call stack as a string e.Source the application or object that threw the exception e.TargetSite the method object that threw the exception Methods e.ToString() returns the name of the exception 7

Throwing an Exception By an invalid operation (implicit exception) Division by 0 Index overflow Acess via a null reference By a throw statement (explicit exception) throw new FunnyException(10); class FunnyException : ApplicationException { public int errorCode; public FunnyException(int x) { errorCode = x; } } 8

Exception Hierarchy 9

Searching for a catch Clause Caller chain is traversed backwards until a method with a matching catch clause is found. If none is found => Program is aborted with a stack trace 10

Example: Handling Exception using try … catch block 11

12

13

Exception class’ Message and StackTrace Properties 14

15

Exception Message: this describes the exception 16

17

Catching Multiple Exceptions using multiple catch blocks 18

19

20

21

Important points about exception handling 22

The Finally Block 23

24

25

26

27

All the user defined exception should be derived from the ApplicationException class We will use this exception in method named Divide( ) 28

29 The code to define custom exception InvalidArgumentException is

Throwing an Exception: the throw keyword 30

31

Handling Overflows with Operators checked and unchecked Checked context The CLR throws an overflowException if overflow occur during calculation Unchecked context The result is of the overflow is truncated Explicit conversions between integral data types can cause overflow 32

using System; class Overflow { static void Main( string[] args ) { int number1 = Int32.MaxValue; // 2,147,483,647 int number2 = Int32.MaxValue; // 2,147,483,647 int sum = 0; Console.WriteLine( "number1: {0}\nnumber2: {1}", number1, number2 ); try { Console.WriteLine( "\nSum integers in checked context:" ); sum = checked( number1 + number2 ); } catch ( OverflowException overflowException ) { Console.WriteLine( overflowException.ToString() ); } Console.WriteLine( "\nsum after checked operation: {0}", sum ); Initialize and declare variables and assigned value to the maximum of int Sum adds number1 and number2 in a checked contextNumber1 and Number2 together causes an overflow, causes overflowException The catch handler gets the overflowException and prints out its string representation 33

Console.WriteLine( "\nSum integers in unchecked context:" ); sum = unchecked( number1 + number2 ); Console.WriteLine( "sum after unchecked operation: {0}", sum ); } // end method Main } // end class Overflow number1: number2: Sum integers in checked context: System.OverflowException: Arithmetic operation resulted in an overflow. at Overflow.Overflow.Main(String[] args) in f:\books\2001\csphtp1\csphtp1_examples\ch11\fig11_09\ overflow\overflow.cs:line 24 sum after checked operation: 0 Sum integers in unchecked context: sum after unchecked operation: -2 Addition of number1 and number2 in unchecked context Sum of the numbers in an unchecked context 34