SlideShare une entreprise Scribd logo
1  sur  22
[object Object],[object Object]
What is exception? Exceptions are unusual error conditions that occur during execution of a program or an application. In C#  exception handling is achieved using the try ? catch ? finally block. All the three are C# keywords that are used do exception handling. The try block encloses the statements that might throw an exception where as catch block handles any exception if one exists. The finally block can be used for doing any clean up process. try { // Statements that are can cause exception } catch(Type x) { // Statements to handle exception } finally { // Statement to clean up  }
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Showing an Error Message to User This kind of Exception Handling mechanism is the most commonly used mechanism.  try { // Code where we are anticipating the Exception } catch (Exception) { System.Windows.Forms.MessageBox.Show("Error Message: This is the Message shown to the User"); } The message shown to the user can be an error message, warning message or an Information message.
What are Custom Exceptions? Why do we need them? Custom Exceptions are exceptions that are created for applying the Business rules of the Application. These exceptions are used to implement the business rules violations of the application. For example:  The minimum balance set for Savings A/C in a bank would be different from a Current A/C. Hence, while developing the application for the same the Business rule would be kept as the same. Violation of Business Rule:  Whenever a Violation of Business rule is done, it should be notified and the proper message should be shown to the user. This will be handled by a Custom Exception. Let us illustrate the same with an example.
private void ValidateBeforeDebit(float Amount) { { if((Balance – Amount ) < MimimumBalanceNeedToHave) { throw new MimimumBalanceViolationForSavingsAccount(&quot;Current TransactionFailed: Minimum Balance not available&quot;); } Else { Debit(Amount); } } } We have a method ValidateBeforeDebit (). This method ensures the minimum balance. If there is no minimum balance then a Custom Exception of the type MimimumBalanceViolationForSavingsAccount is thrown. We need to handle the method whenever we call the method.
[object Object],[object Object],[object Object],[object Object]
   Example of handled exception: The above program can be modified in the following manner to track the exception. You can see the usage of standard  DivideByZeroException  class using System; class HandledException { public static void Main() { int x = 0; int intTemp = 0; try { intTemp = 100/x; Console.WriteLine(?Program terminated before this statement?); } catch(DivideByZeroException de) { Console.WriteLine(&quot;Division by Zero occurs&quot;);  } finally  { Console.WriteLine(&quot;Result is {0}&quot;, intTemp); } } }   
In the above example the program ends in an expected way instead of program termination. At the time of exception program control passes from exception point inside the try block and enters catch blocks. As the finally block is present, so the statements inside final block are executed. Example of exception un handled in catch block As in C#, the catch block is optional. The following program is perfectly legal in C#.  using System; class HandledException { public static void Main() { int x = 0; int intTemp = 0; try { intTemp = 100/x; Console.WriteLine(&quot;Not executed line&quot;); } finally { Console.WriteLine(&quot;Inside Finally Block&quot;); } Console.WriteLine(&quot;Result is {0}&quot;, intTemp); } }  
   The above example will produce no exception at the compilation time but the program will terminate due to un handled exception. But the thing that is of great interest to all the is that before the termination of the program the final block is executed.  Example of Multiple Catch Blocks:   A try block can throw multiple exceptions, that can handle by using multiple catch blocks. Important point here is that more specialized catch block should come before a generalized one. Otherwise the compiler will show a compilation error.  //C#: Exception Handling: Multiple catch
using System; class HandledException { public static void Main() { int x = 0; int intTemp = 0; try { intTemp = 100/x; Console.WriteLine(&quot;Not executed line&quot;); } catch(DivideByZeroException de) { Console.WriteLine(&quot;DivideByZeroException&quot; ); } catch(Exception ee) { Console.WriteLine(&quot;Exception&quot; ); }
finally { Console.WriteLine(&quot;Finally Block&quot;); } Console.WriteLine(&quot;Result is {0}&quot;, intTemp); } }  Example of Catching all Exceptions      By providing a catch block without a brackets or arguments, we can catch all exceptions occurred inside a try block. Even we can use a catch block with an Exception type parameter to catch all exceptions happened inside the try block since in C#, all exceptions are directly or indirectly inherited from the Exception class.  //C#: Exception Handling: Handling all exceptions
using System; class HandledException { public static void Main() { int x = 0; int intTemp = 0; try { intTemp = 100/x; Console.WriteLine(&quot;Not executed line&quot;); } catch { Console.WriteLine(&quot;oException&quot; ); } Console.WriteLine(&quot;Result is {0}&quot;, intTemp); } } 
The following program handles all exception with Exception object.  //C#: Exception Handling: Handling all exceptions using System; class HandledException { public static void Main() { int x = 0; int intTemp = 0; try { intTemp = 100/x; Console.WriteLine(&quot;Not executed line&quot;); } catch(Exception e) { Console.WriteLine(&quot;oException&quot; ); } Console.WriteLine(&quot;Result is {0}&quot;, intTemp); } } 
Example of Throwing an Exception: C# provides facility to throw an exception programmatically. The 'throw' keyword is used for doing the same. The general form of throwing an exception is as follows.  For example the following statement throw an ArgumentException explicitly.  throw new ArgumentException(&quot;Exception&quot;);  using System; class HandledException { public static void Main() { try { throw new DivideByZeroException(&quot;Invalid Division&quot;); } catch(DivideByZeroException e) { Console.WriteLine(&quot;Exception&quot; ); } Console.WriteLine(&quot;Final Statement that is executed&quot;); }  } 
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
User-defined Exceptions: C#, allows to create user defined exception class this class should be derived from Exception base class. So the user-defined exception classes must inherit from either Exception class or one of its standard derived classes.  //C#: Exception Handling: User defined exceptions using System; class UserDefinedException : Exception { public MyException(string str) { Console.WriteLine(&quot;User defined exception&quot;); } } class HandledException { public static void Main() { try { throw new UserDefinedException (&quot;New User Defined Exception&quot;); }
catch(Exception e) { Console.WriteLine(&quot;Exception caught here&quot; + e.ToString()); } Console.WriteLine(&quot;Final Statement that is executed &quot;); } } 
[object Object]

Contenu connexe

Tendances

Exception Handling In Java
Exception Handling In JavaException Handling In Java
Exception Handling In Javaparag
 
Java - Exception Handling Concepts
Java - Exception Handling ConceptsJava - Exception Handling Concepts
Java - Exception Handling ConceptsVicter Paul
 
Java exception handling
Java exception handlingJava exception handling
Java exception handlingBHUVIJAYAVELU
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javaPratik Soares
 
C# lecture 2: Literals , Variables and Data Types in C#
C# lecture 2: Literals , Variables and Data Types in C#C# lecture 2: Literals , Variables and Data Types in C#
C# lecture 2: Literals , Variables and Data Types in C#Dr.Neeraj Kumar Pandey
 
Java 8 - Features Overview
Java 8 - Features OverviewJava 8 - Features Overview
Java 8 - Features OverviewSergii Stets
 
Exception Handling in C#
Exception Handling in C#Exception Handling in C#
Exception Handling in C#Abid Kohistani
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handlingkamal kotecha
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javaARAFAT ISLAM
 
Multi-threaded Programming in JAVA
Multi-threaded Programming in JAVAMulti-threaded Programming in JAVA
Multi-threaded Programming in JAVAVikram Kalyani
 
Exceptionhandling
ExceptionhandlingExceptionhandling
ExceptionhandlingNuha Noor
 
Java Threads and Concurrency
Java Threads and ConcurrencyJava Threads and Concurrency
Java Threads and ConcurrencySunil OS
 

Tendances (20)

Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
 
Exception Handling In Java
Exception Handling In JavaException Handling In Java
Exception Handling In Java
 
Java - Exception Handling Concepts
Java - Exception Handling ConceptsJava - Exception Handling Concepts
Java - Exception Handling Concepts
 
exception handling
exception handlingexception handling
exception handling
 
Core java
Core javaCore java
Core java
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Java exception-handling
Java exception-handlingJava exception-handling
Java exception-handling
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
 
Java threads
Java threadsJava threads
Java threads
 
C# lecture 2: Literals , Variables and Data Types in C#
C# lecture 2: Literals , Variables and Data Types in C#C# lecture 2: Literals , Variables and Data Types in C#
C# lecture 2: Literals , Variables and Data Types in C#
 
Java 8 - Features Overview
Java 8 - Features OverviewJava 8 - Features Overview
Java 8 - Features Overview
 
Exception Handling in C#
Exception Handling in C#Exception Handling in C#
Exception Handling in C#
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
List in java
List in javaList in java
List in java
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Multi-threaded Programming in JAVA
Multi-threaded Programming in JAVAMulti-threaded Programming in JAVA
Multi-threaded Programming in JAVA
 
Exceptionhandling
ExceptionhandlingExceptionhandling
Exceptionhandling
 
Java Threads and Concurrency
Java Threads and ConcurrencyJava Threads and Concurrency
Java Threads and Concurrency
 

En vedette (20)

Exception handling in asp.net
Exception handling in asp.netException handling in asp.net
Exception handling in asp.net
 
Java - Exception Handling
Java - Exception HandlingJava - Exception Handling
Java - Exception Handling
 
Error handling in ASP.NET
Error handling in ASP.NETError handling in ASP.NET
Error handling in ASP.NET
 
Asp.NET Handlers and Modules
Asp.NET Handlers and ModulesAsp.NET Handlers and Modules
Asp.NET Handlers and Modules
 
Runnable interface.34
Runnable interface.34Runnable interface.34
Runnable interface.34
 
Java exception
Java exception Java exception
Java exception
 
Access Protection
Access ProtectionAccess Protection
Access Protection
 
Exceptions in Java
Exceptions in JavaExceptions in Java
Exceptions in Java
 
Effective Java - Chapter 4: Classes and Interfaces
Effective Java - Chapter 4: Classes and InterfacesEffective Java - Chapter 4: Classes and Interfaces
Effective Java - Chapter 4: Classes and Interfaces
 
Exception Handling Java
Exception Handling JavaException Handling Java
Exception Handling Java
 
Java keywords
Java keywordsJava keywords
Java keywords
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Dynamic method dispatch
Dynamic method dispatchDynamic method dispatch
Dynamic method dispatch
 
Java: Exception
Java: ExceptionJava: Exception
Java: Exception
 
Exception handling
Exception handlingException handling
Exception handling
 
Threads in JAVA
Threads in JAVAThreads in JAVA
Threads in JAVA
 
Exception handling
Exception handlingException handling
Exception handling
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Java packages
Java packagesJava packages
Java packages
 
Operators in java
Operators in javaOperators in java
Operators in java
 

Similaire à Exception handling

MODULE5_EXCEPTION HANDLING.docx
MODULE5_EXCEPTION HANDLING.docxMODULE5_EXCEPTION HANDLING.docx
MODULE5_EXCEPTION HANDLING.docxVeerannaKotagi1
 
JP ASSIGNMENT SERIES PPT.ppt
JP ASSIGNMENT SERIES PPT.pptJP ASSIGNMENT SERIES PPT.ppt
JP ASSIGNMENT SERIES PPT.pptJAYAPRIYAR7
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javaAmbigaMurugesan
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overviewBharath K
 
Aae oop xp_13
Aae oop xp_13Aae oop xp_13
Aae oop xp_13Niit Care
 
Exception handling
Exception handlingException handling
Exception handlingWaqas Abbasi
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++Jayant Dalvi
 
Md07 exceptions&assertion
Md07 exceptions&assertionMd07 exceptions&assertion
Md07 exceptions&assertionRakesh Madugula
 
Class notes(week 8) on exception handling
Class notes(week 8) on exception handlingClass notes(week 8) on exception handling
Class notes(week 8) on exception handlingKuntal Bhowmick
 
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 in C++
Exception Handling in C++Exception Handling in C++
Exception Handling in C++Deepak Tathe
 
1.4 core programming [understand error handling]
1.4 core programming [understand error handling]1.4 core programming [understand error handling]
1.4 core programming [understand error handling]tototo147
 
Exceptions
ExceptionsExceptions
Exceptionsmotthu18
 

Similaire à Exception handling (20)

MODULE5_EXCEPTION HANDLING.docx
MODULE5_EXCEPTION HANDLING.docxMODULE5_EXCEPTION HANDLING.docx
MODULE5_EXCEPTION HANDLING.docx
 
JP ASSIGNMENT SERIES PPT.ppt
JP ASSIGNMENT SERIES PPT.pptJP ASSIGNMENT SERIES PPT.ppt
JP ASSIGNMENT SERIES PPT.ppt
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Exception handling
Exception handlingException handling
Exception handling
 
$Cash
$Cash$Cash
$Cash
 
$Cash
$Cash$Cash
$Cash
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overview
 
Java unit3
Java unit3Java unit3
Java unit3
 
F6dc1 session6 c++
F6dc1 session6 c++F6dc1 session6 c++
F6dc1 session6 c++
 
Aae oop xp_13
Aae oop xp_13Aae oop xp_13
Aae oop xp_13
 
Exception handling
Exception handlingException handling
Exception handling
 
Exceptions
ExceptionsExceptions
Exceptions
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++
 
Md07 exceptions&assertion
Md07 exceptions&assertionMd07 exceptions&assertion
Md07 exceptions&assertion
 
Class notes(week 8) on exception handling
Class notes(week 8) on exception handlingClass notes(week 8) on exception handling
Class notes(week 8) on exception handling
 
Exception handling, Stream Classes, Multithread Programming
Exception handling, Stream Classes, Multithread ProgrammingException handling, Stream Classes, Multithread Programming
Exception handling, Stream Classes, Multithread Programming
 
Exception Handling in C++
Exception Handling in C++Exception Handling in C++
Exception Handling in C++
 
1.4 core programming [understand error handling]
1.4 core programming [understand error handling]1.4 core programming [understand error handling]
1.4 core programming [understand error handling]
 
Exceptions
ExceptionsExceptions
Exceptions
 
6-Error Handling.pptx
6-Error Handling.pptx6-Error Handling.pptx
6-Error Handling.pptx
 

Plus de Iblesoft

Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server iiIblesoft
 
MS SQL Server 1
MS SQL Server 1MS SQL Server 1
MS SQL Server 1Iblesoft
 
Master pages ppt
Master pages pptMaster pages ppt
Master pages pptIblesoft
 
State management
State managementState management
State managementIblesoft
 
State management
State managementState management
State managementIblesoft
 
Validation controls ppt
Validation controls pptValidation controls ppt
Validation controls pptIblesoft
 
Generics n delegates
Generics n delegatesGenerics n delegates
Generics n delegatesIblesoft
 
Data controls ppt
Data controls pptData controls ppt
Data controls pptIblesoft
 
Microsoft.net architecturte
Microsoft.net architecturteMicrosoft.net architecturte
Microsoft.net architecturteIblesoft
 
Asp.net architecture
Asp.net architectureAsp.net architecture
Asp.net architectureIblesoft
 
Delegates and events
Delegates and eventsDelegates and events
Delegates and eventsIblesoft
 
Javascript
JavascriptJavascript
JavascriptIblesoft
 

Plus de Iblesoft (17)

Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server ii
 
MS SQL Server 1
MS SQL Server 1MS SQL Server 1
MS SQL Server 1
 
Master pages ppt
Master pages pptMaster pages ppt
Master pages ppt
 
State management
State managementState management
State management
 
State management
State managementState management
State management
 
Validation controls ppt
Validation controls pptValidation controls ppt
Validation controls ppt
 
Controls
ControlsControls
Controls
 
Ado.net
Ado.netAdo.net
Ado.net
 
Generics n delegates
Generics n delegatesGenerics n delegates
Generics n delegates
 
Ajaxppt
AjaxpptAjaxppt
Ajaxppt
 
Data controls ppt
Data controls pptData controls ppt
Data controls ppt
 
Microsoft.net architecturte
Microsoft.net architecturteMicrosoft.net architecturte
Microsoft.net architecturte
 
Asp.net architecture
Asp.net architectureAsp.net architecture
Asp.net architecture
 
Generics
GenericsGenerics
Generics
 
Delegates and events
Delegates and eventsDelegates and events
Delegates and events
 
Javascript
JavascriptJavascript
Javascript
 
Html ppt
Html pptHtml ppt
Html ppt
 

Dernier

Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Presentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxPresentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxRosabel UA
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataBabyAnnMotar
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationdeepaannamalai16
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 

Dernier (20)

Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Presentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxPresentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptx
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped data
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentation
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 

Exception handling

  • 1.
  • 2. What is exception? Exceptions are unusual error conditions that occur during execution of a program or an application. In C# exception handling is achieved using the try ? catch ? finally block. All the three are C# keywords that are used do exception handling. The try block encloses the statements that might throw an exception where as catch block handles any exception if one exists. The finally block can be used for doing any clean up process. try { // Statements that are can cause exception } catch(Type x) { // Statements to handle exception } finally { // Statement to clean up  }
  • 3.
  • 4.
  • 5.
  • 6.
  • 7. Showing an Error Message to User This kind of Exception Handling mechanism is the most commonly used mechanism. try { // Code where we are anticipating the Exception } catch (Exception) { System.Windows.Forms.MessageBox.Show(&quot;Error Message: This is the Message shown to the User&quot;); } The message shown to the user can be an error message, warning message or an Information message.
  • 8. What are Custom Exceptions? Why do we need them? Custom Exceptions are exceptions that are created for applying the Business rules of the Application. These exceptions are used to implement the business rules violations of the application. For example: The minimum balance set for Savings A/C in a bank would be different from a Current A/C. Hence, while developing the application for the same the Business rule would be kept as the same. Violation of Business Rule: Whenever a Violation of Business rule is done, it should be notified and the proper message should be shown to the user. This will be handled by a Custom Exception. Let us illustrate the same with an example.
  • 9. private void ValidateBeforeDebit(float Amount) { { if((Balance – Amount ) < MimimumBalanceNeedToHave) { throw new MimimumBalanceViolationForSavingsAccount(&quot;Current TransactionFailed: Minimum Balance not available&quot;); } Else { Debit(Amount); } } } We have a method ValidateBeforeDebit (). This method ensures the minimum balance. If there is no minimum balance then a Custom Exception of the type MimimumBalanceViolationForSavingsAccount is thrown. We need to handle the method whenever we call the method.
  • 10.
  • 11.   Example of handled exception: The above program can be modified in the following manner to track the exception. You can see the usage of standard DivideByZeroException class using System; class HandledException { public static void Main() { int x = 0; int intTemp = 0; try { intTemp = 100/x; Console.WriteLine(?Program terminated before this statement?); } catch(DivideByZeroException de) { Console.WriteLine(&quot;Division by Zero occurs&quot;);  } finally  { Console.WriteLine(&quot;Result is {0}&quot;, intTemp); } } }   
  • 12. In the above example the program ends in an expected way instead of program termination. At the time of exception program control passes from exception point inside the try block and enters catch blocks. As the finally block is present, so the statements inside final block are executed. Example of exception un handled in catch block As in C#, the catch block is optional. The following program is perfectly legal in C#.  using System; class HandledException { public static void Main() { int x = 0; int intTemp = 0; try { intTemp = 100/x; Console.WriteLine(&quot;Not executed line&quot;); } finally { Console.WriteLine(&quot;Inside Finally Block&quot;); } Console.WriteLine(&quot;Result is {0}&quot;, intTemp); } }  
  • 13.   The above example will produce no exception at the compilation time but the program will terminate due to un handled exception. But the thing that is of great interest to all the is that before the termination of the program the final block is executed.  Example of Multiple Catch Blocks:   A try block can throw multiple exceptions, that can handle by using multiple catch blocks. Important point here is that more specialized catch block should come before a generalized one. Otherwise the compiler will show a compilation error.  //C#: Exception Handling: Multiple catch
  • 14. using System; class HandledException { public static void Main() { int x = 0; int intTemp = 0; try { intTemp = 100/x; Console.WriteLine(&quot;Not executed line&quot;); } catch(DivideByZeroException de) { Console.WriteLine(&quot;DivideByZeroException&quot; ); } catch(Exception ee) { Console.WriteLine(&quot;Exception&quot; ); }
  • 15. finally { Console.WriteLine(&quot;Finally Block&quot;); } Console.WriteLine(&quot;Result is {0}&quot;, intTemp); } }  Example of Catching all Exceptions     By providing a catch block without a brackets or arguments, we can catch all exceptions occurred inside a try block. Even we can use a catch block with an Exception type parameter to catch all exceptions happened inside the try block since in C#, all exceptions are directly or indirectly inherited from the Exception class.  //C#: Exception Handling: Handling all exceptions
  • 16. using System; class HandledException { public static void Main() { int x = 0; int intTemp = 0; try { intTemp = 100/x; Console.WriteLine(&quot;Not executed line&quot;); } catch { Console.WriteLine(&quot;oException&quot; ); } Console.WriteLine(&quot;Result is {0}&quot;, intTemp); } } 
  • 17. The following program handles all exception with Exception object.  //C#: Exception Handling: Handling all exceptions using System; class HandledException { public static void Main() { int x = 0; int intTemp = 0; try { intTemp = 100/x; Console.WriteLine(&quot;Not executed line&quot;); } catch(Exception e) { Console.WriteLine(&quot;oException&quot; ); } Console.WriteLine(&quot;Result is {0}&quot;, intTemp); } } 
  • 18. Example of Throwing an Exception: C# provides facility to throw an exception programmatically. The 'throw' keyword is used for doing the same. The general form of throwing an exception is as follows.  For example the following statement throw an ArgumentException explicitly.  throw new ArgumentException(&quot;Exception&quot;);  using System; class HandledException { public static void Main() { try { throw new DivideByZeroException(&quot;Invalid Division&quot;); } catch(DivideByZeroException e) { Console.WriteLine(&quot;Exception&quot; ); } Console.WriteLine(&quot;Final Statement that is executed&quot;); } } 
  • 19.
  • 20. User-defined Exceptions: C#, allows to create user defined exception class this class should be derived from Exception base class. So the user-defined exception classes must inherit from either Exception class or one of its standard derived classes.  //C#: Exception Handling: User defined exceptions using System; class UserDefinedException : Exception { public MyException(string str) { Console.WriteLine(&quot;User defined exception&quot;); } } class HandledException { public static void Main() { try { throw new UserDefinedException (&quot;New User Defined Exception&quot;); }
  • 21. catch(Exception e) { Console.WriteLine(&quot;Exception caught here&quot; + e.ToString()); } Console.WriteLine(&quot;Final Statement that is executed &quot;); } } 
  • 22.