SlideShare a Scribd company logo
1 of 24
CSC412: Visual Programming
Lecture #: 4
CDF Unit #: 2
Manzoor Ahmad
manzoorahmad@cuivehari.edu.pk
Instructor:
COMSATS University Islamabad,
Vehari Campus 1
1
Topics
C# Code
Error Handling
Debugging
2
2
C# Code
3
Error Handling
Errors can occur at almost any stage when a program runs, and many
errors might not actually be the fault of your own application.
Using traditional techniques to manually add error-detecting code
around every statement is, time consuming, and error prone in its own
right.
You can also lose sight of the main flow of an application if each
statement requires error-handling logic to manage each possible error
that can occur at every stage.
Fortunately, C# makes it easy to separate the error-handling code
from the code that implements the primary logic of a program by
using exceptions and exception handlers.
C# Code
4
Error Handling
Exceptions are generated when the implicit assumptions made by
your programming logic are violated.
E.g., when a program attempts to connect to a database, it
assumes that the database server is up and running on the
network.
If the server can’t be located, an exception is generated.
It’s important that your application gracefully handles any
exceptions that may occur.
If an exception is not handled, your application will terminate.
C# Code
5
Error Handling
You should incorporate a systematic exception handling process in
your methods.
To facilitate this process, the .NET Framework makes use of
structured exception handling through the try, catch, and finally code
blocks.
When an exception is thrown in the try block, execution transfers to
the catch block. You can use more than one catch block to filter for
specific types of exceptions that may be thrown.
The finally block performs any clean up code that you wish to
execute.
The code in the finally block executes regardless of whether an
exception is thrown.
C# Code
6
Error Handling
To write exception-aware programs, you need to do two things:
 Write your code within a try block (try is a C# keyword). When the code runs,
it attempts to execute all the statements in the try block, and if none of the
statements generates an exception, they all run, one after the other, to
completion. However, if an error condition occurs, execution jumps out of the
try block and into another piece of code designed to catch and handle the
exception—a catch handler.
 Write one or more catch handlers (catch is another C# keyword) immediately
after the try block to handle any possible error conditions. A catch handler is
intended to capture and handle a specific type of exception. If any one of the
statements within the try block causes an error, the runtime throws an
exception. The runtime then examines the catch handlers after the try block
and transfers control directly to the first matching handler.
C# Error Handling
7
try & single catch
This code shows a method of a class
that tries to read from a file that does
not exist.
When the exception is thrown, it is
caught in the catch block.
A catch handler employs syntax
similar to that used by a method
parameter to specify the exception to
be caught.
In the preceding example, when a
Exception is thrown, the ex variable is
populated with an object containing
the details of the exception.
C# Error Handling
8
try & multiple catch
All try blocks require at least one
nested catch block.
You can use the catch block to catch
all exceptions that may occur in the try
block, or you can use it to filter
exceptions based on the type of
exception.
This enables you to dynamically
respond to different exceptions based
on the exception type.
E.g., exceptions based on the different
exceptions that could occur when
trying to read a text file from disk.
C# Error Handling
9
try, catch &
finally
 You can nest a finally block at the end of the
try block, the use of the finally block is
optional.
 The finally block is for any clean up code
that needs to occur, even if an exception is
encountered.
 When the code of the try block is executed
and an exception occurs, processing will
evaluate each catch block until it finds the
appropriate catch condition.
 After the catch block executes, the finally
block will execute.
 If the try block executes and no exception is
encountered, the catch blocks don’t execute,
but the finally block will still get processed.
C# Error Handling
10
Nesting try &
catch
In some cases, you may be able to
correct an exception that occurred
and continue processing the rest of
the code in the try block.
In such case, a try-catch block could
be nested around the line of code
that would cause the exception.
After the exception is handled,
processing would return to the line
of code in the outer try-catch block
immediately after the nested try
block.
C# Code Example
11
Error Handling
C# Code Example
12
Error Handling
C# Error Handling
13
Process to
response exception
After an exception is caught, the
next step in the process is to
determine how to respond to it.
You basically have two options:
either recover from the exception
or pass the exception to the
calling procedure.
The following code demonstrates
how to recover from a
DivideByZeroException by
setting the result to zero:
C# Error Handling
14
Process to
response exception
An exception is passed to the calling procedure using the throw
statement.
You can wrap the exception in a new exception containing
additional information that adds relevancy to the exception.
The following code shows how to wrap a caught exception in a
new one and then pass it up the calling chain:
C# Error Handling
15
Process to
response exception
During code execution, when an inappropriate call is made, e.g.,
a parameter to a method has an invalid value or an parameter
passed to a method causes an exception, you can throw an
exception to inform the calling code of the violation.
C# Error Handling
16
Process to
response exception
You preserve the original exception by using the InnerException
property of the Exception class.
Implementing this exception management policy consistently
throughout the various methods in your application will greatly
enhance your ability to build highly maintainable, flexible, and
successful applications
C# Error Handling
17
.NET Framework
Exception Classes
The Common Language Runtime (CLR) has a set of built-in exception
classes.
The CLR will throw an object instance of the appropriate exception type if
an error occurs while executing code instructions.
All .NET Framework exception classes derive from the SystemException
class, which in turn derives from the Exception class.
These base classes provide functionality needed by all exception classes.
If a program attempts to access an out-of-range array index, the CLR
throws an exception of type IndexOutOfRangeException.
Attempting to use a null reference causes a NullReferenceException.
C# Error Handling
18
.NET Framework
Exception Classes
System.Exception
 System.ApplicationException
 System.SystemException
o System.AccessViolationException
o System.ArgumentException
System.ArgumentNullException
System.ArgumentOutOfRangeExcepti
on
o System.FormatException
o System.IndexOutOfRangeException
o System.InvalidCastException
o System.IO.IOException
System.IO.FileNotFoundException
o System.NotImplementedException
o System.NullReferenceException
o System.OutOfMemoryException
C# Error Handling
19
.NET Framework
Exception Classes
Exception Classes in the System.IO Namespace
Why debugging?
We need Reliable Software
Users Choose Reliability over Price
Deeper Understanding
Helps you learn & write better code in the future
C# Code
20
Debugging
“The whole key to debugging effectively is to avoid the debugger as much
as possible because that’s where you waste all your time.” [John Robbins]
“Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by
definition, not smart enough to debug it.” [Brian Kernighan]
C# Code
21
Debugging
When to use it?
Never on self-code…..
Code you write is undoubtedly PERFECT.
Always on co-worker’s code.
Debugging Basics
What are you trying to find and fix?
Two main types of code errors
 Syntax: Compiler catches most if not all of these for you.
 Semantic or logical: Syntactically correct yet program may “crash and
burn” at run-time!
C# Code
22
Debugging –
breakpoint
Stepping through your code
Starting / Stopping
Breaking
Stepping through your application
o(F10, F11 or Toolbar buttons)
Run to a specific location
oRun To Cursor (right-click
menu)
Situations under which
breakpoints are not
feasible
Timing sensitive issues
Breakpoint triggers too
often
Live Debug not possible
Debugging production
systems
C# Code
23
Debugging –
breakpoint
A simple, yet effective breakpoint is extremely powerful ,and
with just that it alone, 99.46% of all bugs are solved.
What does a breakpoint tell the programmer?
The line where the breakpoint is set ;)
The values of all the relevant variables till that point of code.
24

More Related Content

Similar to 6-Error Handling.pptx

Similar to 6-Error Handling.pptx (20)

$Cash
$Cash$Cash
$Cash
 
$Cash
$Cash$Cash
$Cash
 
Exception
ExceptionException
Exception
 
Exceptions
ExceptionsExceptions
Exceptions
 
Exception handling in ASP .NET
Exception handling in ASP .NETException handling in ASP .NET
Exception handling in ASP .NET
 
Exception handling
Exception handlingException handling
Exception handling
 
Lecture 20-21
Lecture 20-21Lecture 20-21
Lecture 20-21
 
Exception handling in .net
Exception handling in .netException handling in .net
Exception handling in .net
 
Node.JS error handling best practices
Node.JS error handling best practicesNode.JS error handling best practices
Node.JS error handling best practices
 
Exception handling
Exception handlingException handling
Exception handling
 
What is Exception Handling?
What is Exception Handling?What is Exception Handling?
What is Exception Handling?
 
Templates and Exception Handling in C++
Templates and Exception Handling in C++Templates and Exception Handling in C++
Templates and Exception Handling in C++
 
What's the Difference Between Static Analysis and Compiler Warnings?
What's the Difference Between Static Analysis and Compiler Warnings?What's the Difference Between Static Analysis and Compiler Warnings?
What's the Difference Between Static Analysis and Compiler Warnings?
 
Different Techniques Of Debugging Selenium Based Test Scripts.pdf
Different Techniques Of Debugging Selenium Based Test Scripts.pdfDifferent Techniques Of Debugging Selenium Based Test Scripts.pdf
Different Techniques Of Debugging Selenium Based Test Scripts.pdf
 
Switch case looping
Switch case loopingSwitch case looping
Switch case looping
 
VBscript
VBscriptVBscript
VBscript
 
Exception handling
Exception handlingException handling
Exception handling
 
43c
43c43c
43c
 
Exception Handling in C++
Exception Handling in C++Exception Handling in C++
Exception Handling in C++
 
F6dc1 session6 c++
F6dc1 session6 c++F6dc1 session6 c++
F6dc1 session6 c++
 

Recently uploaded

Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsPrecisely
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfngoud9212
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 

Recently uploaded (20)

Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power Systems
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdf
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 

6-Error Handling.pptx

  • 1. CSC412: Visual Programming Lecture #: 4 CDF Unit #: 2 Manzoor Ahmad manzoorahmad@cuivehari.edu.pk Instructor: COMSATS University Islamabad, Vehari Campus 1 1
  • 3. C# Code 3 Error Handling Errors can occur at almost any stage when a program runs, and many errors might not actually be the fault of your own application. Using traditional techniques to manually add error-detecting code around every statement is, time consuming, and error prone in its own right. You can also lose sight of the main flow of an application if each statement requires error-handling logic to manage each possible error that can occur at every stage. Fortunately, C# makes it easy to separate the error-handling code from the code that implements the primary logic of a program by using exceptions and exception handlers.
  • 4. C# Code 4 Error Handling Exceptions are generated when the implicit assumptions made by your programming logic are violated. E.g., when a program attempts to connect to a database, it assumes that the database server is up and running on the network. If the server can’t be located, an exception is generated. It’s important that your application gracefully handles any exceptions that may occur. If an exception is not handled, your application will terminate.
  • 5. C# Code 5 Error Handling You should incorporate a systematic exception handling process in your methods. To facilitate this process, the .NET Framework makes use of structured exception handling through the try, catch, and finally code blocks. When an exception is thrown in the try block, execution transfers to the catch block. You can use more than one catch block to filter for specific types of exceptions that may be thrown. The finally block performs any clean up code that you wish to execute. The code in the finally block executes regardless of whether an exception is thrown.
  • 6. C# Code 6 Error Handling To write exception-aware programs, you need to do two things:  Write your code within a try block (try is a C# keyword). When the code runs, it attempts to execute all the statements in the try block, and if none of the statements generates an exception, they all run, one after the other, to completion. However, if an error condition occurs, execution jumps out of the try block and into another piece of code designed to catch and handle the exception—a catch handler.  Write one or more catch handlers (catch is another C# keyword) immediately after the try block to handle any possible error conditions. A catch handler is intended to capture and handle a specific type of exception. If any one of the statements within the try block causes an error, the runtime throws an exception. The runtime then examines the catch handlers after the try block and transfers control directly to the first matching handler.
  • 7. C# Error Handling 7 try & single catch This code shows a method of a class that tries to read from a file that does not exist. When the exception is thrown, it is caught in the catch block. A catch handler employs syntax similar to that used by a method parameter to specify the exception to be caught. In the preceding example, when a Exception is thrown, the ex variable is populated with an object containing the details of the exception.
  • 8. C# Error Handling 8 try & multiple catch All try blocks require at least one nested catch block. You can use the catch block to catch all exceptions that may occur in the try block, or you can use it to filter exceptions based on the type of exception. This enables you to dynamically respond to different exceptions based on the exception type. E.g., exceptions based on the different exceptions that could occur when trying to read a text file from disk.
  • 9. C# Error Handling 9 try, catch & finally  You can nest a finally block at the end of the try block, the use of the finally block is optional.  The finally block is for any clean up code that needs to occur, even if an exception is encountered.  When the code of the try block is executed and an exception occurs, processing will evaluate each catch block until it finds the appropriate catch condition.  After the catch block executes, the finally block will execute.  If the try block executes and no exception is encountered, the catch blocks don’t execute, but the finally block will still get processed.
  • 10. C# Error Handling 10 Nesting try & catch In some cases, you may be able to correct an exception that occurred and continue processing the rest of the code in the try block. In such case, a try-catch block could be nested around the line of code that would cause the exception. After the exception is handled, processing would return to the line of code in the outer try-catch block immediately after the nested try block.
  • 13. C# Error Handling 13 Process to response exception After an exception is caught, the next step in the process is to determine how to respond to it. You basically have two options: either recover from the exception or pass the exception to the calling procedure. The following code demonstrates how to recover from a DivideByZeroException by setting the result to zero:
  • 14. C# Error Handling 14 Process to response exception An exception is passed to the calling procedure using the throw statement. You can wrap the exception in a new exception containing additional information that adds relevancy to the exception. The following code shows how to wrap a caught exception in a new one and then pass it up the calling chain:
  • 15. C# Error Handling 15 Process to response exception During code execution, when an inappropriate call is made, e.g., a parameter to a method has an invalid value or an parameter passed to a method causes an exception, you can throw an exception to inform the calling code of the violation.
  • 16. C# Error Handling 16 Process to response exception You preserve the original exception by using the InnerException property of the Exception class. Implementing this exception management policy consistently throughout the various methods in your application will greatly enhance your ability to build highly maintainable, flexible, and successful applications
  • 17. C# Error Handling 17 .NET Framework Exception Classes The Common Language Runtime (CLR) has a set of built-in exception classes. The CLR will throw an object instance of the appropriate exception type if an error occurs while executing code instructions. All .NET Framework exception classes derive from the SystemException class, which in turn derives from the Exception class. These base classes provide functionality needed by all exception classes. If a program attempts to access an out-of-range array index, the CLR throws an exception of type IndexOutOfRangeException. Attempting to use a null reference causes a NullReferenceException.
  • 18. C# Error Handling 18 .NET Framework Exception Classes System.Exception  System.ApplicationException  System.SystemException o System.AccessViolationException o System.ArgumentException System.ArgumentNullException System.ArgumentOutOfRangeExcepti on o System.FormatException o System.IndexOutOfRangeException o System.InvalidCastException o System.IO.IOException System.IO.FileNotFoundException o System.NotImplementedException o System.NullReferenceException o System.OutOfMemoryException
  • 19. C# Error Handling 19 .NET Framework Exception Classes Exception Classes in the System.IO Namespace
  • 20. Why debugging? We need Reliable Software Users Choose Reliability over Price Deeper Understanding Helps you learn & write better code in the future C# Code 20 Debugging “The whole key to debugging effectively is to avoid the debugger as much as possible because that’s where you waste all your time.” [John Robbins] “Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” [Brian Kernighan]
  • 21. C# Code 21 Debugging When to use it? Never on self-code….. Code you write is undoubtedly PERFECT. Always on co-worker’s code. Debugging Basics What are you trying to find and fix? Two main types of code errors  Syntax: Compiler catches most if not all of these for you.  Semantic or logical: Syntactically correct yet program may “crash and burn” at run-time!
  • 22. C# Code 22 Debugging – breakpoint Stepping through your code Starting / Stopping Breaking Stepping through your application o(F10, F11 or Toolbar buttons) Run to a specific location oRun To Cursor (right-click menu) Situations under which breakpoints are not feasible Timing sensitive issues Breakpoint triggers too often Live Debug not possible Debugging production systems
  • 23. C# Code 23 Debugging – breakpoint A simple, yet effective breakpoint is extremely powerful ,and with just that it alone, 99.46% of all bugs are solved. What does a breakpoint tell the programmer? The line where the breakpoint is set ;) The values of all the relevant variables till that point of code.
  • 24. 24