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 handling
Iblesoft
 
Java - Exception Handling
Java - Exception HandlingJava - Exception Handling
Java - Exception Handling
Prabhdeep Singh
 

What's hot (20)

Exception handling
Exception handlingException handling
Exception handling
 
Exception handling in ASP .NET
Exception handling in ASP .NETException handling in ASP .NET
Exception handling in ASP .NET
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
 
Java Threads
Java ThreadsJava Threads
Java Threads
 
Exception handling
Exception handling Exception handling
Exception handling
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception Handling In Java
Exception Handling In JavaException Handling In Java
Exception Handling In Java
 
Exception Handling in C#
Exception Handling in C#Exception Handling in C#
Exception Handling in C#
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
 
Exceptionhandling
ExceptionhandlingExceptionhandling
Exceptionhandling
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Java collections
Java collectionsJava collections
Java collections
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Java - Exception Handling
Java - Exception HandlingJava - Exception Handling
Java - Exception Handling
 
L14 exception handling
L14 exception handlingL14 exception handling
L14 exception handling
 
PHP - Introduction to Object Oriented Programming with PHP
PHP -  Introduction to  Object Oriented Programming with PHPPHP -  Introduction to  Object Oriented Programming with PHP
PHP - Introduction to Object Oriented Programming with PHP
 

Viewers also liked

11 exception handling
11   exception handling11   exception handling
11 exception handling
Tuan Ngo
 
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]
tototo147
 
Visual programming lab
Visual programming labVisual programming lab
Visual programming lab
Soumya Behera
 
VC++ Fundamentals
VC++ FundamentalsVC++ Fundamentals
VC++ Fundamentals
ranigiyer
 
.NET Framework Overview
.NET Framework Overview.NET Framework Overview
.NET Framework Overview
Doncho Minkov
 

Viewers also liked (18)

Exception Handling
Exception HandlingException Handling
Exception Handling
 
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

Md07 exceptions&assertion
Md07 exceptions&assertionMd07 exceptions&assertion
Md07 exceptions&assertion
Rakesh Madugula
 

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
 
What is Exception Handling?
What is Exception Handling?What is Exception Handling?
What is Exception Handling?
 
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
 

Recently uploaded

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Krashi Coaching
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
fonyou31
 

Recently uploaded (20)

microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 

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.