SlideShare una empresa de Scribd logo
1 de 11
Exception Handling
Exception Handling in .net
 Exceptions are types that all ultimately derive
from System.Exception.
 Use a try block around the statements that might throw
exceptions.
 Once an exception occurs in the try block, the flow of
control jumps to the first associated exception handler
that is present anywhere in the call stack. In C#,
the catch keyword is used to define an exception
handler.
 If no exception handler for a given exception is present,
the program stops executing with an error message.
Syntax
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.
}
try block
 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.
catch 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:
 Re-throw the same exception
 Throw a different exception, giving richer exception
information to code.
 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.
finally block
 GC does automatic memory clean-up, it only cleans up if there are no
references kept on the object.
 Thus, memory leaks can occur if programmers carelessly 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.
 Exception Classes in C#
 C# exceptions are represented by classes. The exception
classes in C# are mainly directly or indirectly derived from
the System.Exception class. Some of the exception classes
derived from the System.Exception class are
the System.ApplicationException and System.SystemE
xception classes.
 The System.ApplicationException class supports
exceptions generated by application programs. So the
exceptions defined by the programmers should derive from
this class.
 The System.SystemException class is the base class for all
predefined system exception.
System.Exception Properties
 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.
Defined Exception
Exception Class Description
System.IO.IOException Handles I/O errors.
System.IndexOutOfRangeException Handles errors generated when a method refers to an array index
out of range.
System.ArrayTypeMismatchException Handles errors generated when type is mismatched with the array
type.
System.NullReferenceException Handles errors generated from deferencing a null object.
System.DivideByZeroException Handles errors generated from dividing a dividend with zero.
System.InvalidCastException Handles errors generated during typecasting.
System.OutOfMemoryException Handles errors generated from insufficient free memory.
System.StackOverflowException Handles errors generated from stack overflow.
Questions ??

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Exceptions overview
Exceptions overviewExceptions overview
Exceptions overview
 
Exception handling in c
Exception handling in cException handling in c
Exception handling in c
 
exception handling in java
exception handling in javaexception handling in java
exception handling in java
 
Exceptions handling notes in JAVA
Exceptions handling notes in JAVAExceptions handling notes in JAVA
Exceptions handling notes in JAVA
 
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
 
Java review: try catch
Java review: try catchJava review: try catch
Java review: try catch
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Z blue exception
Z blue   exceptionZ blue   exception
Z blue exception
 
Java unit 11
Java unit 11Java unit 11
Java unit 11
 
17 exceptions
17   exceptions17   exceptions
17 exceptions
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
130410107010 exception handling
130410107010 exception handling130410107010 exception handling
130410107010 exception handling
 
Exception handling in java
Exception handling  in javaException handling  in java
Exception handling in java
 
Exception handling
Exception handling Exception handling
Exception handling
 
Interface andexceptions
Interface andexceptionsInterface andexceptions
Interface andexceptions
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
12 exception handling
12 exception handling12 exception handling
12 exception handling
 
Exception handling in java
Exception handling in java Exception handling in java
Exception handling in java
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
 

Destacado

Csphtp1 23
Csphtp1 23Csphtp1 23
Csphtp1 23HUST
 
Csphtp1 10
Csphtp1 10Csphtp1 10
Csphtp1 10HUST
 
Csphtp1 05
Csphtp1 05Csphtp1 05
Csphtp1 05HUST
 
Csphtp1 15
Csphtp1 15Csphtp1 15
Csphtp1 15HUST
 
Csphtp1 08
Csphtp1 08Csphtp1 08
Csphtp1 08HUST
 
Csphtp1 18
Csphtp1 18Csphtp1 18
Csphtp1 18HUST
 
Apparel 2 chap 08
Apparel 2   chap 08Apparel 2   chap 08
Apparel 2 chap 08mrschapman
 
Web2.0 guide by_twlog
Web2.0 guide by_twlogWeb2.0 guide by_twlog
Web2.0 guide by_twlogtwdanny
 

Destacado (8)

Csphtp1 23
Csphtp1 23Csphtp1 23
Csphtp1 23
 
Csphtp1 10
Csphtp1 10Csphtp1 10
Csphtp1 10
 
Csphtp1 05
Csphtp1 05Csphtp1 05
Csphtp1 05
 
Csphtp1 15
Csphtp1 15Csphtp1 15
Csphtp1 15
 
Csphtp1 08
Csphtp1 08Csphtp1 08
Csphtp1 08
 
Csphtp1 18
Csphtp1 18Csphtp1 18
Csphtp1 18
 
Apparel 2 chap 08
Apparel 2   chap 08Apparel 2   chap 08
Apparel 2 chap 08
 
Web2.0 guide by_twlog
Web2.0 guide by_twlogWeb2.0 guide by_twlog
Web2.0 guide by_twlog
 

Similar a 3.C#

Exception Handling in C#
Exception Handling in C#Exception Handling in C#
Exception Handling in C#Abid Kohistani
 
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
 
Unit 4 exceptions and threads
Unit 4 exceptions and threadsUnit 4 exceptions and threads
Unit 4 exceptions and threadsDevaKumari Vijay
 
Java SE 11 Exception Handling
Java SE 11 Exception HandlingJava SE 11 Exception Handling
Java SE 11 Exception HandlingAshwin Shiv
 
Exception handling, Stream Classes, Multithread Programming
Exception handling, Stream Classes, Multithread ProgrammingException handling, Stream Classes, Multithread Programming
Exception handling, Stream Classes, Multithread ProgrammingPrabu U
 
Exception Handling
Exception HandlingException Handling
Exception HandlingSunil OS
 
Exceptions & Its Handling
Exceptions & Its HandlingExceptions & Its Handling
Exceptions & Its HandlingBharat17485
 
Exception Handling In Java Presentation. 2024
Exception Handling In Java Presentation. 2024Exception Handling In Java Presentation. 2024
Exception Handling In Java Presentation. 2024kashyapneha2809
 
Java-Exception Handling Presentation. 2024
Java-Exception Handling Presentation. 2024Java-Exception Handling Presentation. 2024
Java-Exception Handling Presentation. 2024nehakumari0xf
 
MODULE5_EXCEPTION HANDLING.docx
MODULE5_EXCEPTION HANDLING.docxMODULE5_EXCEPTION HANDLING.docx
MODULE5_EXCEPTION HANDLING.docxVeerannaKotagi1
 
Java exception handling
Java exception handlingJava exception handling
Java exception handlingBHUVIJAYAVELU
 
Java Exception.ppt
Java Exception.pptJava Exception.ppt
Java Exception.pptRanjithaM32
 

Similar a 3.C# (20)

Exception Handling in C#
Exception Handling in C#Exception Handling in C#
Exception Handling in C#
 
Presentation1
Presentation1Presentation1
Presentation1
 
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
 
Unit 4 exceptions and threads
Unit 4 exceptions and threadsUnit 4 exceptions and threads
Unit 4 exceptions and threads
 
Java SE 11 Exception Handling
Java SE 11 Exception HandlingJava SE 11 Exception Handling
Java SE 11 Exception Handling
 
Java exceptions
Java exceptionsJava exceptions
Java exceptions
 
Java unit3
Java unit3Java unit3
Java unit3
 
Exception handling, Stream Classes, Multithread Programming
Exception handling, Stream Classes, Multithread ProgrammingException handling, Stream Classes, Multithread Programming
Exception handling, Stream Classes, Multithread Programming
 
Exceptionhandling
ExceptionhandlingExceptionhandling
Exceptionhandling
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Exceptions & Its Handling
Exceptions & Its HandlingExceptions & Its Handling
Exceptions & Its Handling
 
Exception Handling In Java Presentation. 2024
Exception Handling In Java Presentation. 2024Exception Handling In Java Presentation. 2024
Exception Handling In Java Presentation. 2024
 
Java-Exception Handling Presentation. 2024
Java-Exception Handling Presentation. 2024Java-Exception Handling Presentation. 2024
Java-Exception Handling Presentation. 2024
 
MODULE5_EXCEPTION HANDLING.docx
MODULE5_EXCEPTION HANDLING.docxMODULE5_EXCEPTION HANDLING.docx
MODULE5_EXCEPTION HANDLING.docx
 
java exception.pptx
java exception.pptxjava exception.pptx
java exception.pptx
 
summarizer16fev16_Exceptions
summarizer16fev16_Exceptionssummarizer16fev16_Exceptions
summarizer16fev16_Exceptions
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Chapter13 exception handling
Chapter13 exception handlingChapter13 exception handling
Chapter13 exception handling
 
Java Exception.ppt
Java Exception.pptJava Exception.ppt
Java Exception.ppt
 

Más de Raghu nath

Ftp (file transfer protocol)
Ftp (file transfer protocol)Ftp (file transfer protocol)
Ftp (file transfer protocol)Raghu nath
 
Javascript part1
Javascript part1Javascript part1
Javascript part1Raghu nath
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsRaghu nath
 
Selection sort
Selection sortSelection sort
Selection sortRaghu nath
 
Binary search
Binary search Binary search
Binary search Raghu nath
 
JSON(JavaScript Object Notation)
JSON(JavaScript Object Notation)JSON(JavaScript Object Notation)
JSON(JavaScript Object Notation)Raghu nath
 
Stemming algorithms
Stemming algorithmsStemming algorithms
Stemming algorithmsRaghu nath
 
Step by step guide to install dhcp role
Step by step guide to install dhcp roleStep by step guide to install dhcp role
Step by step guide to install dhcp roleRaghu nath
 
Network essentials chapter 4
Network essentials  chapter 4Network essentials  chapter 4
Network essentials chapter 4Raghu nath
 
Network essentials chapter 3
Network essentials  chapter 3Network essentials  chapter 3
Network essentials chapter 3Raghu nath
 
Network essentials chapter 2
Network essentials  chapter 2Network essentials  chapter 2
Network essentials chapter 2Raghu nath
 
Network essentials - chapter 1
Network essentials - chapter 1Network essentials - chapter 1
Network essentials - chapter 1Raghu nath
 
Python chapter 2
Python chapter 2Python chapter 2
Python chapter 2Raghu nath
 
python chapter 1
python chapter 1python chapter 1
python chapter 1Raghu nath
 
Linux Shell Scripting
Linux Shell ScriptingLinux Shell Scripting
Linux Shell ScriptingRaghu nath
 

Más de Raghu nath (20)

Mongo db
Mongo dbMongo db
Mongo db
 
Ftp (file transfer protocol)
Ftp (file transfer protocol)Ftp (file transfer protocol)
Ftp (file transfer protocol)
 
MS WORD 2013
MS WORD 2013MS WORD 2013
MS WORD 2013
 
Msword
MswordMsword
Msword
 
Ms word
Ms wordMs word
Ms word
 
Javascript part1
Javascript part1Javascript part1
Javascript part1
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Selection sort
Selection sortSelection sort
Selection sort
 
Binary search
Binary search Binary search
Binary search
 
JSON(JavaScript Object Notation)
JSON(JavaScript Object Notation)JSON(JavaScript Object Notation)
JSON(JavaScript Object Notation)
 
Stemming algorithms
Stemming algorithmsStemming algorithms
Stemming algorithms
 
Step by step guide to install dhcp role
Step by step guide to install dhcp roleStep by step guide to install dhcp role
Step by step guide to install dhcp role
 
Network essentials chapter 4
Network essentials  chapter 4Network essentials  chapter 4
Network essentials chapter 4
 
Network essentials chapter 3
Network essentials  chapter 3Network essentials  chapter 3
Network essentials chapter 3
 
Network essentials chapter 2
Network essentials  chapter 2Network essentials  chapter 2
Network essentials chapter 2
 
Network essentials - chapter 1
Network essentials - chapter 1Network essentials - chapter 1
Network essentials - chapter 1
 
Python chapter 2
Python chapter 2Python chapter 2
Python chapter 2
 
python chapter 1
python chapter 1python chapter 1
python chapter 1
 
Linux Shell Scripting
Linux Shell ScriptingLinux Shell Scripting
Linux Shell Scripting
 
Perl
PerlPerl
Perl
 

Último

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 

Último (20)

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 

3.C#

  • 2. Exception Handling in .net  Exceptions are types that all ultimately derive from System.Exception.  Use a try block around the statements that might throw exceptions.  Once an exception occurs in the try block, the flow of control jumps to the first associated exception handler that is present anywhere in the call stack. In C#, the catch keyword is used to define an exception handler.  If no exception handler for a given exception is present, the program stops executing with an error message.
  • 3. Syntax 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. }
  • 4. try block  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.
  • 5. catch 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.
  • 6.  Once the catch block that matches the exception is found, you have 3 choices:  Re-throw the same exception  Throw a different exception, giving richer exception information to code.  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.
  • 7. finally block  GC does automatic memory clean-up, it only cleans up if there are no references kept on the object.  Thus, memory leaks can occur if programmers carelessly 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.
  • 8.  Exception Classes in C#  C# exceptions are represented by classes. The exception classes in C# are mainly directly or indirectly derived from the System.Exception class. Some of the exception classes derived from the System.Exception class are the System.ApplicationException and System.SystemE xception classes.  The System.ApplicationException class supports exceptions generated by application programs. So the exceptions defined by the programmers should derive from this class.  The System.SystemException class is the base class for all predefined system exception.
  • 9. System.Exception Properties  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.
  • 10. Defined Exception Exception Class Description System.IO.IOException Handles I/O errors. System.IndexOutOfRangeException Handles errors generated when a method refers to an array index out of range. System.ArrayTypeMismatchException Handles errors generated when type is mismatched with the array type. System.NullReferenceException Handles errors generated from deferencing a null object. System.DivideByZeroException Handles errors generated from dividing a dividend with zero. System.InvalidCastException Handles errors generated during typecasting. System.OutOfMemoryException Handles errors generated from insufficient free memory. System.StackOverflowException Handles errors generated from stack overflow.