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

Slides:



Advertisements
Similar presentations
1 Exceptions: An OO Way for Handling Errors Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept. of Computer Science and Software.
Advertisements

Error-handling using exceptions
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.
Yoshi
Exception Handling. Background In a perfect world, users would never enter data in the wrong form, files they choose to open would always exist, and code.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 2 nd Lecture Pavel Ježek
Errors and Exceptions The objectives of this chapter are: To understand the exception handling mechanism defined in Java To explain the difference between.
Exceptions and Exception Handling Carl Alphonce CSE116 March 9, 2007.
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.
Exception Handling Topics We will discuss the following main topics: – Handling Exceptions – Throwing Exceptions – More about Input/Output Streams.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.
Understand Error Handling Software Development Fundamentals LESSON 1.4.
Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling.
COP INTERMEDIATE JAVA Exception Handling Serialization.
Exception Handling.  What are errors?  What does exception handling allow us to do?  Where are exceptions handled?  What does exception handling facilitate?
Exceptions and Exception Handling (1) Carl Alphonce CSE116.
Exceptions and Exception Handling (2) Carl Alphonce CSE116.
Exceptions and Exception Handling (continued) Carl Alphonce CSE116.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
Exceptions Used to signal errors or unexpected situations to calling code Should not be used for problems that can be dealt with reasonably within local.
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.
Java Software Solutions Foundations of Program Design Sixth Edition
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
Chapter 13 Exception Handling F Claiming Exceptions F Throwing Exceptions F Catching Exceptions F Rethrowing Exceptions  The finally Clause F Cautions.
June 14, 2001Exception Handling in Java1 Richard S. Huntrods June 14, 2001 University of Calgary.
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
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
CIS 270—Application Development II Chapter 13—Exception Handling.
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
COMPUTER PROGRAMMING 2 Exceptions. What are Exceptions? Unexpected events that happen when the code is executing (during runtime). Exceptions are types.
Slides Credit Umair Javed LUMS Web Application Development.
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.
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
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
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 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.
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.
Exceptions Lecture 11 COMP 401, Fall /25/2014.
IMS 3253: Validation and Errors 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Validation and Error Handling Validation.
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.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Introduction to Exceptions in Java CS201, SW Development Methods.
Lecture 5: Exception Handling and Text File I/O Michael Hsu CSULA.
Garbage Collection It Is A Way To Destroy The Unused Objects. To do so, we were using free() function in C language and delete() in C++. But, in java it.
Lecture 11 Dr. Eng. Ibrahim El-Nahry Exception Handling.
Eighth Lecture Exception Handling in Java
Validating User Input Lesson 5.
CNS 3260 C# .NET Software Development
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.
Part B – Structured Exception Handling
Web Design & Development Lecture 7
Exception Handling By: Enas Naffar.
Exception Handling Imran Rashid CTO at ManiWeber Technologies.
Lab 1 Exception Handling.
Presentation transcript:

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

Error ?

 Types of Errors Syntactical Semantic / Logical Runtime

Why Exceptions ?  Many programmers do not test for all possible error conditions Reason ?  Trade Offs ?

Exceptions  “Exceptions provide a clean way to check for errors without cluttering code.”  How ? “Transfer control to treat a special case or handle an error condition.”

Errors and Exceptions  Differences

Exception Handling  “A programming language mechanism designed to handle runtime errors or other problems (exceptions)  inside a computer program.”

Behind the Scenes  Exception Information Table (Per Executable). Array of exception handling information (Per Method) in Exception Information Table. Each entry of array describes a protected block of code, and any exception handlers.  Extremely Efficient Structure No performance penalty in case an exception doesn’t occur.

Exception Handling in C#  Structured Exception Handling Using try, catch, finally Blocks

Structured Exception Handling  try Code that may cause an exception should be placed in a ‘try block’  catch After the ‘try block’, usually one or more catch blocks are written that respond to different exceptions, attempting to handle the exception  finally Code that’ll execute no matter whether an exception occurs or not

Spot Problematic Situations in this Innocent Looking Code StreamReader streamReader = new StreamReader(fileName); string fileContents = streamReader.ReadToEnd(); Console.WriteLine(fileContents); streamReader.Close(); Console.WriteLine("\nThank you for using our software!\n");

Example try { StreamReader streamReader = new StreamReader(fileName); string fileContents = streamReader.ReadToEnd(); Console.WriteLine(fileContents); } catch (FileNotFoundException fnfEx) { Console.WriteLine(fnfEx.Message); Console.WriteLine(fnfEx.StackTrace); } finally { if (streamReader != null) { streamReader.Close(); }

Multiple Catch Blocks try { StreamReader streamReader = new StreamReader(fileName); string fileContents = streamReader.ReadToEnd(); Console.WriteLine(fileContents); } catch (FileNotFoundException fnfEx) { Console.WriteLine(fnfEx.Message); Console.WriteLine(fnfEx.StackTrace); } catch (IOException ioEx) { Console.WriteLine(ioEx.Message); Console.WriteLine(ioEx.StackTrace); } finally { if (streamReader != null) { streamReader.Close(); } Derived Base

Tackling ‘Unknown’ Cases try { StreamReader streamReader = new StreamReader(fileName); string fileContents = streamReader.ReadToEnd(); Console.WriteLine(fileContents); } catch (FileNotFoundException fnfEx) { Console.WriteLine(fnfEx.Message); Console.WriteLine(fnfEx.StackTrace); } catch (IOException ioEx) { Console.WriteLine(ioEx.Message); Console.WriteLine(ioEx.StackTrace); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); } finally { if (streamReader != null) { streamReader.Close(); }

Some Useful Properties  HelpLink Gets or sets the location of the help file associated with the exception  Message Gets the string representation of the error message associated with the exception  Source Gets or sets the name of the application or object that caused the exception  StackTrace Gets the stack trace identifying the location in the code where the exception occurred

Different Shapes of (try-catch-finally)  A try block followed by one or more catch blocks  A try block followed by one finally block (No catch block)  A try block followed by one or more catch blocks and a finally block

Image Source:

Standard Exceptions  Two types of Exceptions Exceptions generated by an executing program Exceptions generated by CLR.

Standard Exceptions  The common language runtime throws System Exception.  Excpetion thrown by a user program is called Application Exception.  The System Exception includes the ExecutionEngineException StackOverFlowException etc.

Application Exception  System.NullReferenceException  Syste.InvalidCastException  Syste.ArrayTypeMismatchException  System.IndexOutOfRangeException  System.OverFlowException

Throwing an Exception Explicitly  The ‘throw’ keyword.  General form throw exception_obj;

Throwing an Exception Explicitly  Following statement throws an Exception explicitly. t hrow new Exception(“Exception Message");

…Ctd  It is NOT recommended that we catch System Exceptions that we cannot handle nor is it good programming practice to throw System Exceptions that we cannot handle in our applications

User Defined Exceptions  In C#, it is possible to create our own exception class.   How? 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

Design Guidelines  Exceptions should be used to communicate exceptional conditions. Don’t use them to communicate events that are expected, such as reaching the end of a file.  If there’s a good predefined exception in the System namespace use that.  If code catches an exception that it isn’t going to handle, Consider whether it should wrap that exception with additional information before re-throwing it.