Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 Error ?

3  Types of Errors Syntactical Semantic / Logical Runtime

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

5 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.”

6 Errors and Exceptions  Differences

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

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

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

10 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

11 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");

12 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(); }

13 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

14 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(); }

15 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

16 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

17 Image Source:

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

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

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

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

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

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

24 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

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


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

Similar presentations


Ads by Google