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

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 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.
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.
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.
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.
 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.
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.
COMPUTER PROGRAMMING 2 Exceptions. What are Exceptions? Unexpected events that happen when the code is executing (during runtime). Exceptions are types.
Exceptions Programming in C# Exceptions CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
Exceptions Syntax, semantics, and pragmatics Exceptions1.
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.
Introduction to Exception Handling and Defensive Programming.
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.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
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.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 2 nd Lecture Pavel Ježek
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
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.
Lecture 11 Dr. Eng. Ibrahim El-Nahry Exception Handling.
CSE 1201 Object Oriented Programming
Chapter 11 – Exception Handling
16 Exception Handling.
Debugging and Handling Exceptions
Chapter 10 – Exception Handling
Syntax, semantics, and pragmatics
Chapter 14: Exception Handling
Exception Handling Chapter 9.
Chapter 11—Exceptions Handling Exceptions Throwing Exceptions.
ATS Application Programming: Java Programming
Exceptions & Error Handling
Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.
Abdulmotaleb El Saddik University of Ottawa
Exception Handling in Java
Exception Handling and Reading / Writing Files
Exception Handling Chapter 9 Edited by JJ.
Part B – Structured Exception Handling
Exception Handling By: Enas Naffar.
Exception Handling in Java
Exceptions.
Programming in C# Lesson 5. Exceptions..
Exception Handling Imran Rashid CTO at ManiWeber Technologies.
Chapter 13 Exception Handling
Lecture 11 Objectives Learn what an exception is.
Problems Debugging is fine and dandy, but remember we divided problems into compile-time problems and runtime problems? Debugging only copes with the former.
Chapter 13 Exception Handling: A Deeper Look
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.
Introduction to Programming
Exceptions (part 2) December 3, 2007 ComS 207: Programming I (in Java)
Java Basics Exception Handling.
Chapter 11: Exception Handling
Java Programming: From Problem Analysis to Program Design, 4e
Presentation transcript:

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

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

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

Creating an Exception 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# Exceptions

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

Catching an Exception A catch block is associated with a try 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 are searched in the order they appear in the code catch blocks for specific types must come before the more general types An Empty catch clause will catch any type catch clauses don’t need a variable name catch(Exception) is ok C# Exceptions

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

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

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

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

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

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

Library Exceptions Feel free to use these: ArithmeticException ArrayTypeMismatchException DivideByZeroException IndexOutOfRangeException InvalidCastException NullReferenceException C# Exceptions

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

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

Design Considerations Class discussion C# Exceptions