Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exception Handling Imran Rashid CTO at ManiWeber Technologies.

Similar presentations


Presentation on theme: "Exception Handling Imran Rashid CTO at ManiWeber Technologies."— Presentation transcript:

1 Exception Handling Imran Rashid CTO at ManiWeber Technologies

2 Outline Introduction to Exception Handling Exception Handling in C#

3 Introduction to Exception Handling

4 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…

5 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

6 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

7 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?

8 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?

9 Introduction to Exception Handling
Exception Handling Control Flow

10 Exception Handling in C#

11 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

12 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

13 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

14 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

15 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:

16 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

17 Exception Handling in C#

18 Exception Handling in C#

19 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.

20 Any ?


Download ppt "Exception Handling Imran Rashid CTO at ManiWeber Technologies."

Similar presentations


Ads by Google