Programming in C# Lesson 5. Exceptions..

Slides:



Advertisements
Similar presentations
Error Handling in.NET Exceptions. Error Handling Old way (Win32 API and COM): MyFunction() { error_1 = doSomething(); if (error_1) display error else.
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.
Mahmoud Rafeek Alfarra Computer Programming || Chapter 2: Exception handling.
Chapter 8Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 8 l Basic Exception Handling »the mechanics of exceptions l.
Lesson 16 Exceptions Lesson Exceptions1. Murphy’s Law Anything that can go wrong will go wrong Lesson Exceptions2.
Jerry Lebowitz. Topics  Provides a facility for a systematic object oriented approach to handling runtime errors ◦ Can also handle runtime errors.
Handling Errors during the Program Execution Svetlin Nakov Telerik Corporation
Understand Error Handling Software Development Fundamentals LESSON 1.4.
Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling.
Slides prepared by Rose Williams, Binghamton University Chapter 9 Exception Handling.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Exception Handling An Exception is an indication of a problem that occurs during a program’s execution. Exception handling enables the programmer to create.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
Introduction to C++ Templates and Exceptions l C++ Function Templates l C++ Class Templates l Exception and Exception Handler.
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.
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
Handling Errors during the Program Execution Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
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.
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.
Object Oriented Programming Elhanan Borenstein Lecture #4.
Contract based programming Using pre- and post-conditions, and object invariants Contract based programming1.
CIS 3301 C# Lesson 13 Interfaces. CIS 3302 Objectives Understand the Purpose of Interfaces. Define an Interface. Use an Interface. Implement Interface.
Exceptions Syntax, semantics, and pragmatics Exceptions1.
Java Software Solutions Lewis and Loftus Chapter 14 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Advanced Flow of Control --
VB.Net - Exceptions Copyright © Martin Schray
Introduction to Exception Handling and Defensive Programming.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
Computer Programming with JAVA Chapter 8. Exception Handling Basic Exception Handling the mechanics of exceptions Defining and Using Exceptions some "simple"
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
Exception-Handling Fundamentals  A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of.
Exception Handling Handling Errors During the Program Execution SoftUni Team Technical Trainers Software University
Text INTRODUCTION TO ASP.NET. InterComm Campaign Guidelines CONFIDENTIAL Simply Server side language Simplified page development model Modular, well-factored,
Exception Handling SWE 344 Internet Protocols & Client Server Programming.
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 11 Dr. Eng. Ibrahim El-Nahry Exception Handling.
Chapter 11 – Exception Handling
16 Exception Handling.
Abstract Class Abstract Class is a class which contains atleast one Pure Virtual function in it. Abstract classes are used to provide an Interface for.
CMSC202 Computer Science II for Majors Lecture 16 – Exceptions
CS 2704 Object Oriented Software Design and Construction
Data Abstraction: The Walls
Exceptions David Rabinowitz.
Coding Defensively Coding Defensively
Syntax, semantics, and pragmatics
Magento Technical Guidelines Eugene Shakhsuvarov, Software Magento
Exceptions C++ Interlude 3
Chapter 14: Exception Handling
CNS 3260 C# .NET Software Development
Exception Handling Chapter 9.
Chapter 17 Templates and Exceptions Part 2
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.
Throwing and catching exceptions
Part B – Structured Exception Handling
Exception Handling By: Enas Naffar.
Programming in C# CHAPTER - 7
Exception Handling Imran Rashid CTO at ManiWeber Technologies.
Chapter 12: Exceptions and Advanced File I/O
Chapter 13 Exception Handling: A Deeper Look
Lecture 9.
Chapter 11: Exception Handling
Exceptions for safe programming.
CMSC 202 Exceptions.
Exception Handling.
More C++ Concepts Exception Handling Templates.
Presentation transcript:

Programming in C# Lesson 5. Exceptions.

Defining “Exception” An exception is when a class member fails to complete the task it is supposed to perform as indicated by its name.

Exceptions Characteristics A classic implementation of the OOP exception model Deliver powerful mechanism for centralized handling of errors and unusual events Substitute procedure-oriented approach, in which each function returns error code Simplify code constructions and maintenance Allow the problematic situations to be processed at multiple levels

Handling Exceptions In C# exception can be handled with try-catch-finally construction

Handling Exceptions: Complete Example

Exceptions in .NET System.Exception is a base class for all exceptions in .NET

System.Exception class System.Exception provides information about the cause of the error or unusual situation via properties: Message – text description of the exception StackTrace – a snapshot of the stack at the moment the exception is thrown InnerException – an exception that caused the current exception (if any)

Custom Exceptions To define a custom exception class it needs to be inherited from System.Exception

Mind the hierarchy! When catching an exception of a particular class all derived exceptions are caught too.

Order catch blocks from specific to general!

Throwing Exceptions Exceptions are thrown by using throw keyword.

How Exceptions Work?

Re-throwing Exceptions

Choosing Exception Type When an invalid parameter is passed to a method: ArgumentException ArgumentNullException ArgumentOutOfRangeException When requested operation is not supported: NotSupportedException When a method is not implemented: NotImplementedException If no suitable standard exception class is available: Create your own exception class

try-finally block

using block

Best Practices Order catch blocks from more specific to more general Do not handle all exceptions disregarding their type When raising an exception always pass to the constructor good explanation messages and the inner exception (if any) Do not swallow exceptions Avoid throwing exceptions for normal situations (as they affect performance)