C# Exceptions 1 CNS 3260 C#.NET Software Development.

Slides:



Advertisements
Similar presentations
(Using.NET Platform) Note: Most of the material in these slides have been adapted from MSDN and wikipedia. By Muhammad Ali.
Advertisements

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
Error Handling in.NET Exceptions. Error Handling Old way (Win32 API and COM): MyFunction() { error_1 = doSomething(); if (error_1) display error else.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 2 nd Lecture Pavel Ježek
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.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
C# Programming: From Problem Analysis to Program Design1 Debugging and Handling Exceptions C# Programming: From Problem Analysis to Program Design 3 rd.
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.
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.
Chapter 11 Debugging and Handling Exceptions
EXCEPTIONS. What’s an exception?? Change the flow of control when something important happens ideally - we catch errors at compile time doesn’t happen.
Jerry Lebowitz. Topics  Provides a facility for a systematic object oriented approach to handling runtime errors ◦ Can also handle runtime errors.
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.
Handling Errors during the Program Execution Svetlin Nakov Telerik Corporation
Understand Error Handling Software Development Fundamentals LESSON 1.4.
Objectives Understanding what an exception is Understanding the heirarchy of exception classes Learn the types of exception and how to catch and handle.
Exceptions and Exception Handling (2) Carl Alphonce CSE116.
Exception Handling An Exception is an indication of a problem that occurs during a program’s execution. Exception handling enables the programmer to create.
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.
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.
 2006 Pearson Education, Inc. All rights reserved Exception Handling.
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.
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
06 Exception Handling. 2 Contents What is an Exception? Exception-handling in Java Types of Exceptions Exception Hierarchy try-catch()-finally Statement.
CIS 270—Application Development II Chapter 13—Exception Handling.
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
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.
Exceptions Programming in C# Exceptions CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
Exceptions Syntax, semantics, and pragmatics Exceptions1.
I NTRODUCTION TO PROGRAMMING Starting Out with Java: From Control Structures through Objects CS 146 Class Notes Fall 10.
VB.Net - Exceptions Copyright © Martin Schray
Exception Handling in JAVA. Introduction Exception is an abnormal condition that arises when executing a program. In the languages that do not support.
Object Oriented Software Development 8. Exceptions, testing and debugging.
Introduction to Exception Handling and Defensive Programming.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
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.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
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.
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.
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 2 nd Lecture Pavel Ježek
Chapter 8-Exception Handling/ Robust Programming.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
ZHANG Yu, Intelligence Engineering Lab at College of Computer Science and Technology in Jllin University 1 Programming in C#.NET.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Exception Handling How to handle the runtime errors.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Exceptions an unusual condition – e.g. division by zero – e.g. file doesn't exist – e.g. illegal type – etc. etc… typically a run-time error – i.e. during.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Introduction to Exceptions in Java CS201, SW Development Methods.
Lecture 11 Dr. Eng. Ibrahim El-Nahry Exception Handling.
Debugging and Handling Exceptions
Syntax, semantics, and pragmatics
CNS 3260 C# .NET Software Development
ATS Application Programming: Java Programming
Presentation transcript:

C# Exceptions 1 CNS 3260 C#.NET Software Development

C# Exceptions2 Exceptions in C# Must inherit from System.Exception Must inherit from System.Exception Standard error handling in C# Standard error handling in C# Are thrown when: Are thrown when: the code reaches a throw statement the code reaches a throw statement System exceptions occur (such as divide by zero) System exceptions occur (such as divide by zero) No checked exceptions or exception specifications No checked exceptions or exception specifications

C# Exceptions3 throw statement Must throw an instance of an exception: Must throw an instance of an exception: throw(new MyException(“Error”)); May be used by itself only in a catch block: May be used by itself only in a catch block: catch catch { throw; throw; }

C# Exceptions4 Creating an Exception Class Inherits from System.Exception or a derived class Inherits from System.Exception or a derived class public class Exception1 : System.Exception { public Exception1(string message) : base(message){} } public class SomeException : Exception1 { public SomeException(string message) : base(message){} }

C# Exceptions5 ApplicationException Exceptions defined within an application should extend (inherit from) System.ApplicationException Exceptions defined within an application should extend (inherit from) System.ApplicationException

C# Exceptions6 Catching an Exception A catch block is associated with a try block A catch block is associated with a try block A try block may have more than one catch block A try block may have more than one catch block catch blocks catch the exception type or any derived exception types passing through catch blocks catch the exception type or any derived exception types passing through catch blocks are searched in the order they appear in the code catch blocks are searched in the order they appear in the code catch blocks for specific types must come before the more general types catch blocks for specific types must come before the more general types An Empty catch clause will catch any type An Empty catch clause will catch any type catch clauses don’t need a variable name catch clauses don’t need a variable name catch(Exception) is ok catch(Exception) is ok

C# Exceptions7 catch blocks void function1() { try { // code } catch(Exception1 ex) { } catch(Exception ex) { } // if no rethrow occurs // execution resumes here } void function1() { try { // code } catch(Exception ex) { } catch(Exception1 ex) { } } Wrong Right

C# Exceptions8 Exception Flow Control void function1() { try { try { throw(new SomeOtherException(“Error Message”)); } catch(Exception1 ex) { } catch(Exception2 ex) { } } The exception is passed up until a suitable handler is found The exception is passed up until a suitable handler is found

C# Exceptions9 Exception Flow Control void function2() { try { Function1(); } catch(Exception3 ex3) { } catch(Exception2 ex4) { } catch(Exception ex) { } } If no suitable handler (catch clause) was found, the exception is passed to the calling method If no suitable handler (catch clause) was found, the exception is passed to the calling method

C# Exceptions10 Unhandled Exceptions If no error handler is found the application terminates If no error handler is found the application terminates Control is passed back to Windows Control is passed back to Windows

C# Exceptions11 finally block Must be associated with a try block Must be associated with a try block a try block may have only one finally block a try block may have only one finally block finally block always gets executed finally block always gets executed The appropriate catch clause is executed first The appropriate catch clause is executed first (See Exceptions Demo)

C# Exceptions12 finally Flow Control void function1() { try { try { throw(new SomeException(“Error Message”)); } catch(Exception1 ex) { } finally { } catch(Exception2 ex) { } finally { }

C# Exceptions13 Some Special Rules Unhandled Exception in a destructor Unhandled Exception in a destructor destructor stops executing, exception is discarded, base destructor is called destructor stops executing, exception is discarded, base destructor is called catch (with no parameter) will catch unmanaged exceptions from other languages catch (with no parameter) will catch unmanaged exceptions from other languages

C# Exceptions14 Library Exceptions Feel free to use these: Feel free to use these: ArithmeticException ArithmeticException ArrayTypeMismatchException ArrayTypeMismatchException DivideByZeroException DivideByZeroException IndexOutOfRangeException IndexOutOfRangeException InvalidCastException InvalidCastException NullReferenceException NullReferenceException OutOfMemoryException OutOfMemoryException OverflowException OverflowException StackOverflowException StackOverflowException TypeInitializationException TypeInitializationException

C# Exceptions15 System.Exception Class Message Message string message associated with the exception string message associated with the exception InnerException InnerException If this exception was generated inside an exception handler, this refers to the original exception If this exception was generated inside an exception handler, this refers to the original exception Source Source Refers to the source class Refers to the source class StackTrace StackTrace String representing the call stack, file and line number String representing the call stack, file and line number

C# Exceptions16 Breaking On Exceptions Debug | Exceptions (or Ctrl + Alt + E) Debug | Exceptions (or Ctrl + Alt + E)

C# Exceptions17 Design Considerations Class discussion Class discussion