Programming in C# CHAPTER - 7

Slides:



Advertisements
Similar presentations
Topics Introduction Types of Errors Exceptions Exception Handling
Advertisements

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.
Index Exception handling Exception In Java Exception Types
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.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 13 In a language without exception handling: When an exception occurs, control goes to the operating.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
CS 3260 Dennis A. Fairclough Version 1.0
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Slides prepared by Rose Williams, Binghamton University Chapter 9 Exception Handling.
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 prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
Exception Error handling. Exception 4 n An unusual occurrence during program execution that requires immediate handling n Errors are the most common type.
1 Exception Handling Introduction to Exception Handling Exception Handling in PLs –Ada –C++ –Java Sebesta Chapter 14.
June 14, 2001Exception Handling in Java1 Richard S. Huntrods June 14, 2001 University of Calgary.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 9 : Exception Handling King Fahd University of Petroleum & Minerals College of Computer.
Object Oriented Programming
CIS 270—Application Development II Chapter 13—Exception Handling.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Slides Credit Umair Javed LUMS Web Application Development.
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.
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.
Exceptions. Why exceptions? We often strive for writing portable reusable code; we are able to detect errors, however our code may be used for many different.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
CSCI 383 Object-Oriented Programming & Design Lecture 20 Martin van Bommel.
Exception Handling How to handle the runtime errors.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
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.
Lecture 18B Exception Handling and Richard Gesick.
Lecture 11 Dr. Eng. Ibrahim El-Nahry Exception Handling.
Object Throwable ErrorException RuntimeException.
Eighth Lecture Exception Handling in Java
The Object-Oriented Thought Process Chapter 03
C ++ MULTIPLE CHOICE QUESTION
Chapter 11 – Exception Handling
Java Exceptions a quick review….
C++ Exceptions.
16 Exception Handling.
Exception Handling and Event Handling
Exception Handling and Event Handling
EXCEPTION HANDLING IN C++
Tirgul 13 Exceptions 1.
Why exception handling in C++?
Exception Handling and Event Handling
Functions.
EXCEPTION HANDLING.
Haidong Xue Summer 2011, at GSU
Chapter 14: Exception Handling
Exception Handling and
Exception Handling Chapter 9.
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 in Java
Exception Handling Chapter 9 Edited by JJ.
Part B – Structured Exception Handling
Exception Handling By: Enas Naffar.
Exceptions.
Exception Handling Imran Rashid CTO at ManiWeber Technologies.
Managing Errors and Exceptions
Problems Debugging is fine and dandy, but remember we divided problems into compile-time problems and runtime problems? Debugging only copes with the former.
Python Syntax Errors and Exceptions
Introduction to Programming
Debugging and Handling Exceptions
CMSC 202 Exceptions 2nd Lecture.
Exception Handling and Event Handling
CMSC 202 Exceptions.
Exception Handling.
Exceptions.
Exceptions and networking
Presentation transcript:

Programming in C# CHAPTER - 7

Exception Handling Exception Handling Exception handling is an in built mechanism in .NET framework to detect and handle run time errors. The .NET framework contains lots of standard exceptions. The exceptions are anomalies that occur during the execution of a program. They can be because of user, logic or system errors. If a user (programmer) do not provide a mechanism to handle these anomalies, the .NET run time environment provide a default mechanism, which terminates the program execution. C# provides three keywords try, catch and finally to do exception handling. The try encloses the statements that might throw an exception whereas catch handles an exception if one exists. The finally can be used for doing any clean up process.

Try, Catch and Finally blocks Exception Handling general form try-catch-finally in C# is shown below: try { // Statement which can cause an exception. } catch(Type x) { // Statements for handling the exception finally { //Any cleanup code }

Exception Handling Mechanism Exception Handling mechanism Both catch and finally blocks are optional. The try block can exist either with one or more catch blocks or a finally block or with both catch and finally blocks. If there is no exception occurred inside the try block, the control directly transfers to finally block. We can say that the statements inside the finally block is executed always. Note that it is an error to transfer control out of a finally block by using break, continue, return or goto.

Exceptions Exceptions In C#, exceptions are nothing but objects of the type Exception class. The Exception is the ultimate base class for any exceptions in C#. The C# itself provides couple of standard exceptions. Or even the user can create their own exception classes, provided that this should inherit from either Exception class or one of the standard derived classes of Exception class like DivideByZeroExcpetion of ArgumentException etc.

Finally Block The finally block is useful for cleaning up any resources allocated in the try block as well as running any code that must execute even if there is an exception. Control is always passed to the finally block regardless of how the try block exits. Whereas catch is used to handle exceptions that occur in a statement block, finally is used to guarantee an executable block of code regardless of how the preceding try block is exited.

Predefined Exception Classes Standard Exceptions System.OutOfMemoryException System.NullReferenceException System.InvalidCastException System.ArrayTypeMismatchException System.IndexOutOfRangeException System.ArithmeticException System.DevideByZeroException System.OverFlowException

Catching all Exceptions By providing a catch block without a brackets or arguments, we can catch all exceptions occurred inside a try block. Even we can use a catch block with an Exception type parameter to catch all exceptions happened inside the try block. since in C#, all exceptions are directly or indirectly inherited from the Exception class.

Designing your Own Exception User Defined Exceptions In C# It is possible to create our own exception class. But Exception must be the ultimate base class for all exceptions in C#. So the user-defined exception classes must inherit from either Exception class or one of its standard derived classes.

Check & Unchecked Statements The checked keyword is used to explicitly enable overflow-checking for integral-type arithmetic operations and conversions. By default, if an expression produces a value that is outside the range of the destination type. Constant expressions cause compile-time errors, and non-constant expressions are evaluated at run-time and raise exceptions. However, the checked keyword can be used to enable checking .of overflow and display the message instead of showing an erroneous result.

Check & Unchecked Statements The unchecked keyword is used to suppress overflow-checking for integral-type arithmetic operations and conversions. In an unchecked context, if an expression produces a value that is outside the range of the destination type, the result is truncated.