Exception Handling Imran Rashid CTO at ManiWeber Technologies.

Slides:



Advertisements
Similar presentations
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Advertisements

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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Exceptions Briana B. Morrison CSE 1302C Spring 2010.
CSI 3120, Exception handling, page 1 Exception and Event Handling Credits Robert W. Sebesta, Concepts of Programming Languages, 8 th ed., 2007 Dr. Nathalie.
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Chapter 11 Exception Handling and Event Handling.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
1 Exception and Event Handling (Based on:Concepts of Programming Languages, 8 th edition, by Robert W. Sebesta, 2007)
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.
Preventing and Correcting Errors
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.
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
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.
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.
CIS 3301 C# Lesson 13 Interfaces. CIS 3302 Objectives Understand the Purpose of Interfaces. Define an Interface. Use an Interface. Implement Interface.
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
1 An Exception is… An unusual, often unpredictable event, detectable by software or hardware, that requires special processing An exception handler is.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
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.
Exceptions Lecture 11 COMP 401, Fall /25/2014.
Exception Handling SWE 344 Internet Protocols & Client Server Programming.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Exceptions in OO Programming Introduction Errors Exceptions in Java Handling exceptions The Try-Catch-Finally mechanism Example code Exception propagation.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
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.
Object Throwable ErrorException RuntimeException.
Lec.11 (Chapter 11) Exception Jiang (Jen) ZHENG July 13 th, 2005.
Eighth Lecture Exception Handling in Java
Java Exceptions a quick review….
C++ Exceptions.
Exception Handling and Event Handling
Exception Handling and Event Handling
PZ11A Programming Language design and Implementation -4th Edition
Exception and Event Handling
Introduction to Exceptions in Java
Exception Handling and Event Handling
Chapter 14: Exception Handling
Handling Exceptions.
Assignments A sample main program for Project 2 phase 2 is contained in: It reads.
Exception Handling Chapter 9.
Exceptions Handling the unexpected
Exceptions & Error Handling
Exception Handling and Event Handling
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
Exception Handling and Event Handling
Java Exceptions Dan Fleck CS211.
Exception handling Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.1.
Ninth step for Learning C++ Programming
Tenth step for Learning C++ Programming
Exception and Event Handling
Exceptions 10-May-19.
Exception handling Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.1.
Java Basics Exception Handling.
Chapter 11: Exception Handling
Exception Handling and Event Handling
CMSC 202 Exceptions.
Exception Handling.
Exception handling Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.1.
Exception handling Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.1.
Presentation transcript:

Exception Handling Imran Rashid CTO at ManiWeber Technologies

Outline Introduction to Exception Handling Exception Handling in C#

Introduction to Exception Handling

Introduction to Exception Handling In a language without exception handling When an exception occurs, control goes to the operating system, where a message is shown & program terminated In a language with exception handling Programs are allowed to trap some exceptions, thereby providing the possibility of fixing it and continuing try { statement 1; statement 2; … } catch(exceptionType1) { statements… } catch(exceptionType2) { } statements after exception handling…

Introduction to Exception Handling Basic Concepts Many languages allow programs to trap errors An exception is any unusual event, either erroneous or not, detectable by either hardware or software, that may require special processing The special processing that may be required after detection of an exception is called exception handling The exception handling code is called exception handler

Introduction to Exception Handling Advantages of Built-in Exception Handling Error detection code is hard to write and it clutters the program Exception handling encourages programmers to consider many different possible errors Exception propagation allows a high level of reuse of exception handling code Maintain the normal flow of the application Separating error-handling code from regular code Meaningful error reporting

Introduction to Exception Handling Design Issues How and where are exception handlers specified and what is their scope? How is an exception occurrence bound to an exception handler? Can information about the exception be passed to the handler? Where does execution continue, if at all, after an exception handler completes its execution? (continuation vs. resumption) Is some form of finalization provided?

Introduction to Exception Handling How are user-defined exceptions specified? Should there be default exception handlers for programs that do not provide their own? Can predefined exceptions be explicitly raised? Are hardware-detectable errors treated as exceptions that can be handled? Are there any predefined exceptions? How can exceptions be disabled, if at all?

Introduction to Exception Handling Exception Handling Control Flow

Exception Handling in C#

Exception Handling in C# Exceptions Exceptions are unforeseen errors that happen in your programs Most of the time, you can, and should, detect and handle program errors in your code For example: Validating user input Checking for null objects Verifying the values returned from methods are what you expect All examples of good standard error handling that you should be doing all the time

Exception Handling in C# However, there are times when you don’t know if an error will occur For example: You can’t predict when you’ll receive a file I/O error Run out of system memory Encounter a database error Array out of index error These things are generally unlikely, but they could still happen and you want to be able to deal with them when they do occur This is where exception handling comes in

Exception Handling in C# When exceptions occur, they are said to be “thrown” What is actually thrown is an object that is derived from the System.Exception class The System.Exception class provides several methods and properties for obtaining information on what went wrong For example: The Message property provides summary information about what the error was The Stacktrace property provides information from the stack for where the problem occurred The ToString() method is overridden to reveal a verbose description of the entire exception

Exception Handling in C# try/catch Blocks Exceptions are handled by implementing a try/catch block Code that could throw an exception is put in the try block and exception handling code goes in the catch block

Exception Handling in C# Although the code in last slide only has a single catch block, all exceptions will be caught there because the type is of the base exception type “Exception” In exception handling, more specific exceptions will be caught before their more general parent exceptions For example, the following snippet shows how to place multiple catch blocks:

Exception Handling in C# Finally Blocks An exception can leave your program in an inconsistent state by not releasing resources or doing some other type of cleanup A catch block is a good place to figure out what may have gone wrong and try to recover, however it can’t account for all scenarios Sometimes you need to perform clean up actions whether or not your program succeeds These situations are good candidates for using a finally block

Exception Handling in C#

Exception Handling in C#

ASSIGNMENT Submission Date 05/11/2018 & in written form Q1: How to handle exceptions in C++? Code at least 2 different examples. Q2: How to handle exceptions in Java? Code at least 2 different examples. Q3: How to handle exceptions in PHP? Code at least 2 different examples.

Any ?