SlideShare a Scribd company logo
1 of 18
Exception Handling in C#
Anul Chaudhary
131CC00304
Contents
Example
Exception Classes in c#
Syntax
Exception handling
What is an exception error?
2
What is an exception ?
 An exception is a problem that arises
during the execution of a program.
 A C# exception is a response to an
exceptional situation that arises while a
program is running, such as an attempt to
divide by zero.
3
 Exceptions provide a way to transfer
control from one part of a program to
another.
4
Exception handling
 C# exception handling is built upon four keywords:
 try: A try block identifies a block of code for
which particular exceptions will be activated.
It's followed by one or more catch blocks.
 catch: A program catches an exception with an
exception handler at the place in a program
where you want to handle the problem. The
catch keyword indicates the catching of an
exception.
5
 finally: The finally block is used to execute a given
set of statements, whether an exception is thrown or
not thrown.
For example, if you open a file, it must be closed
whether an exception is raised or not.
 throw: A program throws an exception when a
problem shows up. This is done using a throw
keyword.
www.cst.ps/staff/mfarra
www.facebook.com/mahmoudRfarra
6
Syntax
 Assuming a block will raise and exception, a
method catches an exception using a combination
of the try and catch keywords.
 You can list down multiple catch statements to
catch different type of exceptions in case your try
block raises more than one exception in different
situations.
7
working of try & catch
Try
block
Catch
block
Statement
that
Cause
Exception
Statement
that
Cause
Exception
working of try & catch with
Finally
Try Block
Copyright © 2013 Ezzat Harki
Finally
Catch BlockFinally
Exception Classes in C#
 Exceptions in .NET are objects
 The System.Exception class is base
for all exceptions in CLR
 Contains information for the cause of
the error / unusual situation
Message – text description of the
exception
StackTrace – the snapshot of the stack
at the moment of exception throwing
InnerException – exception caused
the current
exception (if any)
 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.
13
Exception Classes in C#
14
Example 1
1. class Program {
2. public static void division(int num1, int num2)
3. {
4. float result=0.0f;
5. try
6. {
7. result = num1 / num2;
8. }
9. catch (DivideByZeroException e)
10. {
11. Console.WriteLine("Exception Error !! n divide by zero !!");
12. // Console.WriteLine("Exception caught: {0}", e);
13. }
1. finally
2. {
3. Console.WriteLine("Result: {0} ", result);
4. }
5. }
6. static void Main(string[] args)
7. {
8. division(10,0);
9. Console.ReadLine();
10. } }
Conclusion
The conclusion is that by using try, Catch and finally block
we can avoid exception occurring in the program and can
also display its type.
Thank You

More Related Content

What's hot

Exception handling
Exception handlingException handling
Exception handlingIblesoft
 
VB.NET:An introduction to Namespaces in .NET framework
VB.NET:An introduction to  Namespaces in .NET frameworkVB.NET:An introduction to  Namespaces in .NET framework
VB.NET:An introduction to Namespaces in .NET frameworkRicha Handa
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling pptJavabynataraJ
 
Exception Handling
Exception HandlingException Handling
Exception HandlingReddhi Basu
 
Dependency injection presentation
Dependency injection presentationDependency injection presentation
Dependency injection presentationAhasanul Kalam Akib
 
Java Comments | Java course
Java Comments | Java courseJava Comments | Java course
Java Comments | Java courseRAKESH P
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivityVaishali Modi
 
Java DataBase Connectivity API (JDBC API)
Java DataBase Connectivity API (JDBC API)Java DataBase Connectivity API (JDBC API)
Java DataBase Connectivity API (JDBC API)Luzan Baral
 
Exceptions in Java
Exceptions in JavaExceptions in Java
Exceptions in JavaVadym Lotar
 

What's hot (20)

Exception handling in java
Exception handling  in javaException handling  in java
Exception handling in java
 
Exception handling
Exception handlingException handling
Exception handling
 
What is Exception Handling?
What is Exception Handling?What is Exception Handling?
What is Exception Handling?
 
Java Annotations
Java AnnotationsJava Annotations
Java Annotations
 
SQLITE Android
SQLITE AndroidSQLITE Android
SQLITE Android
 
Callback Function
Callback FunctionCallback Function
Callback Function
 
VB.NET:An introduction to Namespaces in .NET framework
VB.NET:An introduction to  Namespaces in .NET frameworkVB.NET:An introduction to  Namespaces in .NET framework
VB.NET:An introduction to Namespaces in .NET framework
 
Exception handling
Exception handlingException handling
Exception handling
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling ppt
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Introduction to Spring Boot
Introduction to Spring BootIntroduction to Spring Boot
Introduction to Spring Boot
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Java exception
Java exception Java exception
Java exception
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Dependency injection presentation
Dependency injection presentationDependency injection presentation
Dependency injection presentation
 
Java Comments | Java course
Java Comments | Java courseJava Comments | Java course
Java Comments | Java course
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivity
 
JDBC – Java Database Connectivity
JDBC – Java Database ConnectivityJDBC – Java Database Connectivity
JDBC – Java Database Connectivity
 
Java DataBase Connectivity API (JDBC API)
Java DataBase Connectivity API (JDBC API)Java DataBase Connectivity API (JDBC API)
Java DataBase Connectivity API (JDBC API)
 
Exceptions in Java
Exceptions in JavaExceptions in Java
Exceptions in Java
 

Viewers also liked

Viewers also liked (17)

Programming in c#
Programming in c#Programming in c#
Programming in c#
 
11 exception handling
11   exception handling11   exception handling
11 exception handling
 
Understanding Exception Handling in .Net
Understanding Exception Handling in .NetUnderstanding Exception Handling in .Net
Understanding Exception Handling in .Net
 
1.3 core programming [identify the appropriate method for handling repetition]
1.3 core programming [identify the appropriate method for handling repetition]1.3 core programming [identify the appropriate method for handling repetition]
1.3 core programming [identify the appropriate method for handling repetition]
 
Console applications IN C#
Console applications IN C#Console applications IN C#
Console applications IN C#
 
01. introduction to-programming
01. introduction to-programming01. introduction to-programming
01. introduction to-programming
 
C# - Part 1
C# - Part 1C# - Part 1
C# - Part 1
 
Visual programming lab
Visual programming labVisual programming lab
Visual programming lab
 
Window programming
Window programmingWindow programming
Window programming
 
VC++ Fundamentals
VC++ FundamentalsVC++ Fundamentals
VC++ Fundamentals
 
C# basics
 C# basics C# basics
C# basics
 
.NET Framework Overview
.NET Framework Overview.NET Framework Overview
.NET Framework Overview
 
Exception handling
Exception handlingException handling
Exception handling
 
Introduction To Dotnet
Introduction To DotnetIntroduction To Dotnet
Introduction To Dotnet
 
Jdbc Ppt
Jdbc PptJdbc Ppt
Jdbc Ppt
 
Programming windows
Programming windowsProgramming windows
Programming windows
 
Windows programming ppt
Windows programming pptWindows programming ppt
Windows programming ppt
 

Similar to Presentation1

Similar to Presentation1 (20)

Exceptions
ExceptionsExceptions
Exceptions
 
Exceptions
ExceptionsExceptions
Exceptions
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overview
 
Java exceptions
Java exceptionsJava exceptions
Java exceptions
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Exception handling basic
Exception handling basicException handling basic
Exception handling basic
 
Exceptionhandling
ExceptionhandlingExceptionhandling
Exceptionhandling
 
Md07 exceptions&assertion
Md07 exceptions&assertionMd07 exceptions&assertion
Md07 exceptions&assertion
 
JAVA PPT -4 BY ADI.pdf
JAVA PPT -4 BY ADI.pdfJAVA PPT -4 BY ADI.pdf
JAVA PPT -4 BY ADI.pdf
 
UNIT-3.pptx Exception Handling and Multithreading
UNIT-3.pptx Exception Handling and MultithreadingUNIT-3.pptx Exception Handling and Multithreading
UNIT-3.pptx Exception Handling and Multithreading
 
Introduction to Exception
Introduction to ExceptionIntroduction to Exception
Introduction to Exception
 
6-Error Handling.pptx
6-Error Handling.pptx6-Error Handling.pptx
6-Error Handling.pptx
 
3.C#
3.C#3.C#
3.C#
 
Java -Exception handlingunit-iv
Java -Exception handlingunit-ivJava -Exception handlingunit-iv
Java -Exception handlingunit-iv
 
Java unit3
Java unit3Java unit3
Java unit3
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Exception
ExceptionException
Exception
 
Java SE 11 Exception Handling
Java SE 11 Exception HandlingJava SE 11 Exception Handling
Java SE 11 Exception Handling
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception handling
Exception handlingException handling
Exception handling
 

Recently uploaded

What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
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
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxabhijeetpadhi001
 
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
 
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
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
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
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 

Recently uploaded (20)

What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
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
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
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
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptx
 
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
 
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
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
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
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 

Presentation1

  • 1. Exception Handling in C# Anul Chaudhary 131CC00304
  • 2. Contents Example Exception Classes in c# Syntax Exception handling What is an exception error? 2
  • 3. What is an exception ?  An exception is a problem that arises during the execution of a program.  A C# exception is a response to an exceptional situation that arises while a program is running, such as an attempt to divide by zero. 3
  • 4.  Exceptions provide a way to transfer control from one part of a program to another. 4
  • 5. Exception handling  C# exception handling is built upon four keywords:  try: A try block identifies a block of code for which particular exceptions will be activated. It's followed by one or more catch blocks.  catch: A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception. 5
  • 6.  finally: The finally block is used to execute a given set of statements, whether an exception is thrown or not thrown. For example, if you open a file, it must be closed whether an exception is raised or not.  throw: A program throws an exception when a problem shows up. This is done using a throw keyword. www.cst.ps/staff/mfarra www.facebook.com/mahmoudRfarra 6
  • 7. Syntax  Assuming a block will raise and exception, a method catches an exception using a combination of the try and catch keywords.  You can list down multiple catch statements to catch different type of exceptions in case your try block raises more than one exception in different situations. 7
  • 8.
  • 9. working of try & catch Try block Catch block Statement that Cause Exception Statement that Cause Exception
  • 10. working of try & catch with Finally Try Block Copyright © 2013 Ezzat Harki Finally Catch BlockFinally
  • 11. Exception Classes in C#  Exceptions in .NET are objects  The System.Exception class is base for all exceptions in CLR  Contains information for the cause of the error / unusual situation Message – text description of the exception StackTrace – the snapshot of the stack at the moment of exception throwing InnerException – exception caused the current exception (if any)
  • 12.
  • 13.  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. 13
  • 15. Example 1 1. class Program { 2. public static void division(int num1, int num2) 3. { 4. float result=0.0f; 5. try 6. { 7. result = num1 / num2; 8. } 9. catch (DivideByZeroException e) 10. { 11. Console.WriteLine("Exception Error !! n divide by zero !!"); 12. // Console.WriteLine("Exception caught: {0}", e); 13. }
  • 16. 1. finally 2. { 3. Console.WriteLine("Result: {0} ", result); 4. } 5. } 6. static void Main(string[] args) 7. { 8. division(10,0); 9. Console.ReadLine(); 10. } }
  • 17. Conclusion The conclusion is that by using try, Catch and finally block we can avoid exception occurring in the program and can also display its type.