SlideShare a Scribd company logo
1 of 22
T.DEEPIKA
MSC(INFO-TECH)
NADAR SARASWATHI COLLEGE OF ARTS AND
SCIENCE
 Old way (Win32 API and COM):
MyFunction()
{
error_1 = doSomething();
if (error_1)
display error
else
{
continue processing
if (error_2)
display error
else
continue processing
}
}
 It uses Exceptions
 Exception handling enables programmers to remove
error-handling code from the “main line” of the program’s
execution.
 Examples:
◦ Without exceptions: DivideByZeroNoExceptionHandling.cs
◦ With exceptions: DivideByZeroTest.sln
try {
// code that requires common cleanup or
// exception-recovery operations
}
catch (InvalidOperationException) {
//code that recovers from an InvalidOperationException
// (or any exception type derived from it)
}
catch (SomeOtherException) {
// code that recovers from an SomeOtherException
// (or any exception type derived from it)
}
catch {
// code that recovers from any kind of exception
// when you catch any exception, you usually re-throw
throw;
}
finally {
// code that cleans up any operations started
// within the try block. This code ALWAYS executes.
}
 A try block contains code that requires common cleanup or
exception-recovery operations.
 The cleanup code should be put in a single finally block.
 The exception recovery code should be put in one or more
catch blocks.
◦ Create one catch block for each kind of type you want to handle.
 A try block must have at least one catch or finally
block.
 A catch block contains code to execute in response to
an exception.
 If the code in a try block doesn’t cause an exception to
be thrown, the CLR will never execute the code in any of
its catch blocks.
 You may or may not specify a catch type in
parantheses after catch :
◦ The catch type must be of type System.Exception or a type
that derived from System.Exception
◦ If there is no catch type specified, that catch block handles any
exception. This is equivalent to having a catch block that
specifies System.Exception as a catch type.
◦ CLR searches for a matching catch type from top to bottom.
◦ If CLR cannot find any catch type that matches the exception, CLR
continues searching up the callstack to find a catch type.
 Once the catch block that matches the exception is
found, you have 3 choices:
1. Re-throw the same exception, notifying the higher-up call stack
of the exception
2. Throw a different exception, giving richer exception information
to code higher-up in the call stack
3. Let the code continue from the bottom of the catch block
 In choices 1-2, an exception is thrown and code starts
looking for a catch block whose type matches the
exception thrown
 In choice 3, the finally block is executed
 You can also specify a variable name like
catch(Exception e) to access information specific
to the exception.
 The CLR does not completely eliminate memory leaks. Why?
 Even though GC does automatic memory clean-up, it only
cleans up if there are no references kept on the object.
 Even then there may be a delay until the memory is required.
 Thus, memory leaks can occur if programmers inadvertently
keep references to unwanted objects.
 C# provides the finally block, which is guaranteed to
execute regardless of whether an exception occurs.
If the try block executes without throwing, the finally block
executes.
If the try block throws an exception, the finally block still executes
regardless of whether the exception is caught.
 This makes the finally block ideal to release resources from
the corresponding try block.
 Example: UsingExceptions.cs
 Local variables in a try block cannot be accessed in the
corresponding finally block, so variables that must be
accessed in both should be declared before the try block.
 Placing the finally block before a catch block is a syntax
error.
 A try block does not require a finally block, sometimes no
clean-up is needed.
 A try block can have no more than one finally block.
 Avoid putting code that might throw in a finally block.
◦ Exception handling will still work but the CLR will not keep the information
about the first exception just thrown in the corresponding try block.
The using statement simplifies writing code in which you
obtain a resource.
The general form of a using statement is:
using ( ExampleObject e = new ExampleObject() )
{
e.SomeMethod();
}
This using statement code is equivalent to:
{
ExampleObject e = new ExampleObject();
try
{
e.SomeMethod();
}
finally
{
if ( e != null )
( ( IDisposable ) e ).Dispose();
}
}
 In .NET, only objects of class Exception and its derived
classes may be thrown and caught.
 Exceptions thrown in other .NET languages can be caught with
the general catch clause.
 Class Exception is the base class of .NET’s exception class
hierarchy.
 A catch block can use a base-class type to catch a hierarchy
of related exceptions.
 A catch block that specifies a parameter of type
Exception can catch all exceptions.
Class Exception’s properties are used to formulate error
messages indicating a caught exception.
Property Message stores the error message associated with an
Exception object.
Property StackTrace contains a string that represents the method-
call stack.
When an exception occurs, a programmer might use a
different error message or indicate a new exception type.
The original exception object is stored in the InnerException
property.
Other properties:
HelpLink specifies the location of a help file that describes the
problem.
Source specifies the name of the application or object that caused
the exception.
TargetSite specifies the method where the exception originated.
The CLR generates SystemExceptions, derived from class
Exception, which can occur at any point during program
execution.
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.
 System.Exception
System.ApplicationException
System.SystemException
System.AccessViolationException
System.ArgumentException
System.ArgumentNullException
System.ArgumentOutOfRangeException
System.FormatException
System.IndexOutOfRangeException
System.InvalidCastException
System.IO.IOException
System.IO.FileNotFoundException
System.NotImplementedException
System.NullReferenceException
System.OutOfMemoryException
Example: Convert.ToInt32
Search for “Convert.ToInt32” in the Index of the
Visual Studio online documentation.
Select the document entitled Convert.ToInt32 Method.
In the document that describes the method, click the link
ToInt32(String).
The Exceptions section indicates that method
Convert.ToInt32 throws two exception types.
 They are not an “exceptional event”, a rare event that occurs.
 They are not just errors.
 They are specific results returned when a method could not
complete its task.
 Example: when should Transfer throw?
public class Account {
public void Transfer(Account from, Account to,
decimal amount) {
…
}
}
 When the Transfer method detects any of such possible
conditions and cannot transfer the money, then it should
notify the caller that it failed by throwing an exception.
 When implementing your own methods, you should throw an
exception when the method cannot complete its task.
 Associating each type of malfunction with an appropriately
named exception class improves program clarity.
1. What Exception-derived type are you going to throw?
 You must select a meaningful type
 You can select a type defined in FCL that matches your semantics
 If not, you may need to define your own type
 Your exception type hierarchy should be shallow and wide: that is, create
as few base classes as possible
1. What string message are you going to pass to the exception
type’s constructor?
 If the exception is handled, no one will see this exception message.
 If the exception is unhandled, then the code will probably log this
message and a developer would want to understand what went wrong
using this message. So the message should give enough detail as
possible.
 Message does not need to be localized.
 User-defined exception classes should derive directly or
indirectly from class Exception of namespace System.
 Exceptions should be documented so that other
developers will know how to handle them.
 User-defined exceptions should define three constructors:
◦ a parameterless constructor
◦ a constructor that receives a string argument
(the error message)
◦ a constructor that receives a string argument and an Exception
argument (the error message and the inner exception object)
 SquareRootTest
try {
// code that might fail…
}
catch (Exception) {
…
}
 How can you write code that can recover from all
situations???
 A class library should never ever swallow all exceptions. The
application should get a chance to handle the exception.
 You can catch all exceptions only if you are going to process it
and re-throw it again.
 The ability to keep cleanup code in a dedicated location and making sure
this cleanup code will execute
 The ability to keep code that deals with exceptional situations in a central
place
 The ability to locate and fix bugs in the code
 Unified error handling: all .NET Framework classes throw exceptions to
handle error cases
 Old Win32 APIs and COM returns a 32-bit error code. Exceptions include
a string description of the problem.
 Exceptions also include a stack trace that tells you the path application
took until the error occurred.
 You can also put any information you want in a user-defined exception of
your own.
 The caller could ignore the error returned by a Win32 API, now the caller
cannot continue with exceptions. If the application cannot handle the
exception, CLR can terminate the application.
THANK YOU

More Related Content

What's hot

Exception Handling In Java
Exception Handling In JavaException Handling In Java
Exception Handling In Javaparag
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handlingkamal kotecha
 
14 exception handling
14 exception handling14 exception handling
14 exception handlingjigeno
 
Exception Handling Java
Exception Handling JavaException Handling Java
Exception Handling Javaankitgarg_er
 
Understanding Exception Handling in .Net
Understanding Exception Handling in .NetUnderstanding Exception Handling in .Net
Understanding Exception Handling in .NetMindfire Solutions
 
Exception handling chapter15
Exception handling chapter15Exception handling chapter15
Exception handling chapter15Kumar
 
02 Symbian Os Basics Tipos De Dados
02 Symbian Os Basics Tipos De Dados02 Symbian Os Basics Tipos De Dados
02 Symbian Os Basics Tipos De DadosTiago Romão
 
Exception handling
Exception handlingException handling
Exception handlingpooja kumari
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handlingAlpesh Oza
 
Exception Handling
Exception HandlingException Handling
Exception HandlingPRN USM
 
Effective Java Second Edition
Effective Java Second EditionEffective Java Second Edition
Effective Java Second Editionlosalamos
 
Exception handling and templates
Exception handling and templatesException handling and templates
Exception handling and templatesfarhan amjad
 

What's hot (20)

Exception Handling In Java
Exception Handling In JavaException Handling In Java
Exception Handling In Java
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
14 exception handling
14 exception handling14 exception handling
14 exception handling
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception Handling Java
Exception Handling JavaException Handling Java
Exception Handling Java
 
Understanding Exception Handling in .Net
Understanding Exception Handling in .NetUnderstanding Exception Handling in .Net
Understanding Exception Handling in .Net
 
Exception handling chapter15
Exception handling chapter15Exception handling chapter15
Exception handling chapter15
 
Exception handlingpdf
Exception handlingpdfException handlingpdf
Exception handlingpdf
 
12 exception handling
12 exception handling12 exception handling
12 exception handling
 
Presentation1
Presentation1Presentation1
Presentation1
 
Exception handling
Exception handlingException handling
Exception handling
 
Exceptionhandling
ExceptionhandlingExceptionhandling
Exceptionhandling
 
Z blue exception
Z blue   exceptionZ blue   exception
Z blue exception
 
Java
JavaJava
Java
 
02 Symbian Os Basics Tipos De Dados
02 Symbian Os Basics Tipos De Dados02 Symbian Os Basics Tipos De Dados
02 Symbian Os Basics Tipos De Dados
 
Exception handling
Exception handlingException handling
Exception handling
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handling
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Effective Java Second Edition
Effective Java Second EditionEffective Java Second Edition
Effective Java Second Edition
 
Exception handling and templates
Exception handling and templatesException handling and templates
Exception handling and templates
 

Similar to Exceptions

Exception Handling Mechanism in .NET CLR
Exception Handling Mechanism in .NET CLRException Handling Mechanism in .NET CLR
Exception Handling Mechanism in .NET CLRKiran Munir
 
Md07 exceptions&assertion
Md07 exceptions&assertionMd07 exceptions&assertion
Md07 exceptions&assertionRakesh Madugula
 
Exception Handling in C#
Exception Handling in C#Exception Handling in C#
Exception Handling in C#Abid Kohistani
 
12. Exception Handling
12. Exception Handling 12. Exception Handling
12. Exception Handling Intro C# Book
 
9781439035665 ppt ch11
9781439035665 ppt ch119781439035665 ppt ch11
9781439035665 ppt ch11Terry Yoast
 
Chapter 8 - Exceptions and Assertions Edit summary
Chapter 8 - Exceptions and Assertions  Edit summaryChapter 8 - Exceptions and Assertions  Edit summary
Chapter 8 - Exceptions and Assertions Edit summaryEduardo Bergavera
 
Interface andexceptions
Interface andexceptionsInterface andexceptions
Interface andexceptionssaman Iftikhar
 
Exception handling
Exception handlingException handling
Exception handlingIblesoft
 
Exceptions ref
Exceptions refExceptions ref
Exceptions ref. .
 
Java -Exception handlingunit-iv
Java -Exception handlingunit-ivJava -Exception handlingunit-iv
Java -Exception handlingunit-ivRubaNagarajan
 
Java SE 11 Exception Handling
Java SE 11 Exception HandlingJava SE 11 Exception Handling
Java SE 11 Exception HandlingAshwin Shiv
 
Java Exception.ppt
Java Exception.pptJava Exception.ppt
Java Exception.pptRanjithaM32
 
Exception handling, Stream Classes, Multithread Programming
Exception handling, Stream Classes, Multithread ProgrammingException handling, Stream Classes, Multithread Programming
Exception handling, Stream Classes, Multithread ProgrammingPrabu U
 

Similar to Exceptions (20)

Exception Handling Mechanism in .NET CLR
Exception Handling Mechanism in .NET CLRException Handling Mechanism in .NET CLR
Exception Handling Mechanism in .NET CLR
 
Md07 exceptions&assertion
Md07 exceptions&assertionMd07 exceptions&assertion
Md07 exceptions&assertion
 
Exception Handling in C#
Exception Handling in C#Exception Handling in C#
Exception Handling in C#
 
12. Exception Handling
12. Exception Handling 12. Exception Handling
12. Exception Handling
 
Chap12
Chap12Chap12
Chap12
 
9781439035665 ppt ch11
9781439035665 ppt ch119781439035665 ppt ch11
9781439035665 ppt ch11
 
Chapter 8 - Exceptions and Assertions Edit summary
Chapter 8 - Exceptions and Assertions  Edit summaryChapter 8 - Exceptions and Assertions  Edit summary
Chapter 8 - Exceptions and Assertions Edit summary
 
Interface andexceptions
Interface andexceptionsInterface andexceptions
Interface andexceptions
 
$Cash
$Cash$Cash
$Cash
 
$Cash
$Cash$Cash
$Cash
 
Java unit3
Java unit3Java unit3
Java unit3
 
Exception handling
Exception handlingException handling
Exception handling
 
Java exceptions
Java exceptionsJava exceptions
Java exceptions
 
Java: Exception
Java: ExceptionJava: Exception
Java: Exception
 
Exceptions ref
Exceptions refExceptions ref
Exceptions ref
 
6-Error Handling.pptx
6-Error Handling.pptx6-Error Handling.pptx
6-Error Handling.pptx
 
Java -Exception handlingunit-iv
Java -Exception handlingunit-ivJava -Exception handlingunit-iv
Java -Exception handlingunit-iv
 
Java SE 11 Exception Handling
Java SE 11 Exception HandlingJava SE 11 Exception Handling
Java SE 11 Exception Handling
 
Java Exception.ppt
Java Exception.pptJava Exception.ppt
Java Exception.ppt
 
Exception handling, Stream Classes, Multithread Programming
Exception handling, Stream Classes, Multithread ProgrammingException handling, Stream Classes, Multithread Programming
Exception handling, Stream Classes, Multithread Programming
 

More from DeepikaT13

Mobile computing
Mobile computingMobile computing
Mobile computingDeepikaT13
 
Image processing
Image processingImage processing
Image processingDeepikaT13
 
Spatial filtering
Spatial filteringSpatial filtering
Spatial filteringDeepikaT13
 
Hive architecture
Hive  architectureHive  architecture
Hive architectureDeepikaT13
 
Sotware engineering
Sotware engineeringSotware engineering
Sotware engineeringDeepikaT13
 
Computer network
Computer networkComputer network
Computer networkDeepikaT13
 
Storage management in operating system
Storage management in operating systemStorage management in operating system
Storage management in operating systemDeepikaT13
 
Neural network
Neural networkNeural network
Neural networkDeepikaT13
 
memory reference instruction
memory reference instructionmemory reference instruction
memory reference instructionDeepikaT13
 
breadth first search
breadth first searchbreadth first search
breadth first searchDeepikaT13
 
Computer registers
Computer registersComputer registers
Computer registersDeepikaT13
 

More from DeepikaT13 (20)

Mobile computing
Mobile computingMobile computing
Mobile computing
 
Image processing
Image processingImage processing
Image processing
 
aloha
alohaaloha
aloha
 
Spatial filtering
Spatial filteringSpatial filtering
Spatial filtering
 
Hive architecture
Hive  architectureHive  architecture
Hive architecture
 
Rdbms
RdbmsRdbms
Rdbms
 
Sotware engineering
Sotware engineeringSotware engineering
Sotware engineering
 
Data mining
Data miningData mining
Data mining
 
Computer network
Computer networkComputer network
Computer network
 
Storage management in operating system
Storage management in operating systemStorage management in operating system
Storage management in operating system
 
Jdbc
JdbcJdbc
Jdbc
 
Data mining
Data miningData mining
Data mining
 
Neural network
Neural networkNeural network
Neural network
 
memory reference instruction
memory reference instructionmemory reference instruction
memory reference instruction
 
breadth first search
breadth first searchbreadth first search
breadth first search
 
constructors
constructorsconstructors
constructors
 
Disjoint set
Disjoint setDisjoint set
Disjoint set
 
Destructors
DestructorsDestructors
Destructors
 
Crisp set
Crisp setCrisp set
Crisp set
 
Computer registers
Computer registersComputer registers
Computer registers
 

Recently uploaded

Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 

Recently uploaded (20)

Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 

Exceptions

  • 2.  Old way (Win32 API and COM): MyFunction() { error_1 = doSomething(); if (error_1) display error else { continue processing if (error_2) display error else continue processing } }
  • 3.  It uses Exceptions  Exception handling enables programmers to remove error-handling code from the “main line” of the program’s execution.  Examples: ◦ Without exceptions: DivideByZeroNoExceptionHandling.cs ◦ With exceptions: DivideByZeroTest.sln
  • 4. try { // code that requires common cleanup or // exception-recovery operations } catch (InvalidOperationException) { //code that recovers from an InvalidOperationException // (or any exception type derived from it) } catch (SomeOtherException) { // code that recovers from an SomeOtherException // (or any exception type derived from it) } catch { // code that recovers from any kind of exception // when you catch any exception, you usually re-throw throw; } finally { // code that cleans up any operations started // within the try block. This code ALWAYS executes. }
  • 5.  A try block contains code that requires common cleanup or exception-recovery operations.  The cleanup code should be put in a single finally block.  The exception recovery code should be put in one or more catch blocks. ◦ Create one catch block for each kind of type you want to handle.  A try block must have at least one catch or finally block.
  • 6.  A catch block contains code to execute in response to an exception.  If the code in a try block doesn’t cause an exception to be thrown, the CLR will never execute the code in any of its catch blocks.  You may or may not specify a catch type in parantheses after catch : ◦ The catch type must be of type System.Exception or a type that derived from System.Exception ◦ If there is no catch type specified, that catch block handles any exception. This is equivalent to having a catch block that specifies System.Exception as a catch type. ◦ CLR searches for a matching catch type from top to bottom. ◦ If CLR cannot find any catch type that matches the exception, CLR continues searching up the callstack to find a catch type.
  • 7.  Once the catch block that matches the exception is found, you have 3 choices: 1. Re-throw the same exception, notifying the higher-up call stack of the exception 2. Throw a different exception, giving richer exception information to code higher-up in the call stack 3. Let the code continue from the bottom of the catch block  In choices 1-2, an exception is thrown and code starts looking for a catch block whose type matches the exception thrown  In choice 3, the finally block is executed  You can also specify a variable name like catch(Exception e) to access information specific to the exception.
  • 8.  The CLR does not completely eliminate memory leaks. Why?  Even though GC does automatic memory clean-up, it only cleans up if there are no references kept on the object.  Even then there may be a delay until the memory is required.  Thus, memory leaks can occur if programmers inadvertently keep references to unwanted objects.  C# provides the finally block, which is guaranteed to execute regardless of whether an exception occurs. If the try block executes without throwing, the finally block executes. If the try block throws an exception, the finally block still executes regardless of whether the exception is caught.  This makes the finally block ideal to release resources from the corresponding try block.  Example: UsingExceptions.cs
  • 9.  Local variables in a try block cannot be accessed in the corresponding finally block, so variables that must be accessed in both should be declared before the try block.  Placing the finally block before a catch block is a syntax error.  A try block does not require a finally block, sometimes no clean-up is needed.  A try block can have no more than one finally block.  Avoid putting code that might throw in a finally block. ◦ Exception handling will still work but the CLR will not keep the information about the first exception just thrown in the corresponding try block.
  • 10. The using statement simplifies writing code in which you obtain a resource. The general form of a using statement is: using ( ExampleObject e = new ExampleObject() ) { e.SomeMethod(); } This using statement code is equivalent to: { ExampleObject e = new ExampleObject(); try { e.SomeMethod(); } finally { if ( e != null ) ( ( IDisposable ) e ).Dispose(); } }
  • 11.  In .NET, only objects of class Exception and its derived classes may be thrown and caught.  Exceptions thrown in other .NET languages can be caught with the general catch clause.  Class Exception is the base class of .NET’s exception class hierarchy.  A catch block can use a base-class type to catch a hierarchy of related exceptions.  A catch block that specifies a parameter of type Exception can catch all exceptions.
  • 12. Class Exception’s properties are used to formulate error messages indicating a caught exception. Property Message stores the error message associated with an Exception object. Property StackTrace contains a string that represents the method- call stack. When an exception occurs, a programmer might use a different error message or indicate a new exception type. The original exception object is stored in the InnerException property. Other properties: HelpLink specifies the location of a help file that describes the problem. Source specifies the name of the application or object that caused the exception. TargetSite specifies the method where the exception originated.
  • 13. The CLR generates SystemExceptions, derived from class Exception, which can occur at any point during program execution. 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.
  • 15. Example: Convert.ToInt32 Search for “Convert.ToInt32” in the Index of the Visual Studio online documentation. Select the document entitled Convert.ToInt32 Method. In the document that describes the method, click the link ToInt32(String). The Exceptions section indicates that method Convert.ToInt32 throws two exception types.
  • 16.  They are not an “exceptional event”, a rare event that occurs.  They are not just errors.  They are specific results returned when a method could not complete its task.  Example: when should Transfer throw? public class Account { public void Transfer(Account from, Account to, decimal amount) { … } }  When the Transfer method detects any of such possible conditions and cannot transfer the money, then it should notify the caller that it failed by throwing an exception.
  • 17.  When implementing your own methods, you should throw an exception when the method cannot complete its task.  Associating each type of malfunction with an appropriately named exception class improves program clarity. 1. What Exception-derived type are you going to throw?  You must select a meaningful type  You can select a type defined in FCL that matches your semantics  If not, you may need to define your own type  Your exception type hierarchy should be shallow and wide: that is, create as few base classes as possible 1. What string message are you going to pass to the exception type’s constructor?  If the exception is handled, no one will see this exception message.  If the exception is unhandled, then the code will probably log this message and a developer would want to understand what went wrong using this message. So the message should give enough detail as possible.  Message does not need to be localized.
  • 18.  User-defined exception classes should derive directly or indirectly from class Exception of namespace System.  Exceptions should be documented so that other developers will know how to handle them.  User-defined exceptions should define three constructors: ◦ a parameterless constructor ◦ a constructor that receives a string argument (the error message) ◦ a constructor that receives a string argument and an Exception argument (the error message and the inner exception object)
  • 20. try { // code that might fail… } catch (Exception) { … }  How can you write code that can recover from all situations???  A class library should never ever swallow all exceptions. The application should get a chance to handle the exception.  You can catch all exceptions only if you are going to process it and re-throw it again.
  • 21.  The ability to keep cleanup code in a dedicated location and making sure this cleanup code will execute  The ability to keep code that deals with exceptional situations in a central place  The ability to locate and fix bugs in the code  Unified error handling: all .NET Framework classes throw exceptions to handle error cases  Old Win32 APIs and COM returns a 32-bit error code. Exceptions include a string description of the problem.  Exceptions also include a stack trace that tells you the path application took until the error occurred.  You can also put any information you want in a user-defined exception of your own.  The caller could ignore the error returned by a Win32 API, now the caller cannot continue with exceptions. If the application cannot handle the exception, CLR can terminate the application.

Editor's Notes

  1. from or to could be null from or to object might not refer to an open account from account might have insufficient funds to account might cause an overflow  the amount might be 0 or negative