SlideShare a Scribd company logo
Understanding
Exception Handling in .net.

Presenter: Harsh Wardhan, Mindfire Solutions
Date: 29/10/2013
Agenda
- What is Exception?
- The try, the catch & the finally.
- A look into System.Exception class.
- Throwing Exception.
- Defining your own exception class.
- Best practices.
- Unhandled Exceptions.
- Debugging Exceptions.
Presenter: Harsh Wardhan, Mindfire Solutions
What is Exception?
- When a member fails to complete the task it is
supposed to perform as indicated by its name, it is
called an exception.

Presenter: Harsh Wardhan, Mindfire Solutions
The Try
- A Try block contains code that require:
- Common clean up operations.
- Exception recovery operations.
- A code that might potentially throw an
exception.
- A Try block should have at least one catch or finally
block associated with it.
- How much code should be added in a single try
block?
Presenter: Harsh Wardhan, Mindfire Solutions
The Catch
- Contains code to execute in response to an
exception.
- A try block can have multiple catch blocks.
- The expression after Catch keyword is catch type.
- CLR searches the catch type from top to bottom.

Presenter: Harsh Wardhan, Mindfire Solutions
The Catch
- What to do in a catch block?
– Re-throw same exception.
– Throw a different exception with richer
exception information to code higher up in the
call stack.
– Let the thread fall out of the bottom of the
catch block.

Presenter: Harsh Wardhan, Mindfire Solutions
The Finally
- It contains code that's guranteed to execute.
- Mostly contains clean up operations required by
actions taken in try block.
- Code in catch and finally blocks should be short
and should have likelihood of succeeding without
itself throwing an exception.

Presenter: Harsh Wardhan, Mindfire Solutions
A look into System.Exception
- Base class for all exceptions.
- All user and system defined exceptions derive from
System.Exception class.
- Important Properties of System.Exception:
- Message
- Data
- Source
- Stack-trace
- Target Site
- Help link
- Inner exception
Presenter: Harsh Wardhan, Mindfire Solutions
Throwing Exception
- Points to consider while throwing an exception:
– What exception derived type you need to
throw.
– What string message are you going to pass
to the exception type's constructor.

Presenter: Harsh Wardhan, Mindfire Solutions
Defining your own Exception
class
- Create your exception class when the error is
tightly bound to the class issuing the error.
- Your exception class should be shallow and wide.

Presenter: Harsh Wardhan, Mindfire Solutions
Defining your own Exception
class
- Best practices for creating Exception class:
– Should derive from ApplicationException
– Marked with <System.Seralizable> attribute
– Defines a default constructor
– Defines a constructor that sets inherited
message property.
– Defines a constructor to handle inner exception.
– Defines a constructor for serialization of your
type.
Presenter: Harsh Wardhan, Mindfire Solutions
Best Practices
- Use
–
–
–

finally block liberally.
Use finally to clean up from any operation.
Dispose objects to avoid resource leak.
Examples in our language compiler:
• Lock statement.
• Using statement.
• For each statement.

Presenter: Harsh Wardhan, Mindfire Solutions
Best Practices
- Don't Catch everything.
– When you catch an exception you are
actually saying one of these things:
• You expected this exeption.
• You understand why it occurred.
• You know how to deal with it.

Presenter: Harsh Wardhan, Mindfire Solutions
Best Practices
- NEVER leave an empty catch block.
- Always try to catch specific exceptions.
- Catch a System.Exception and execute some code
inside catch block as long as you re-throw the
exception.

Presenter: Harsh Wardhan, Mindfire Solutions
Unhandled Exceptions
- If no catch block matches the thrown exception
type, an unhandled exception occurs.
- Usually information gets logged in Event Log.

Presenter: Harsh Wardhan, Mindfire Solutions
Debugging Exceptions
- You can enable the exception to break the flow
when it occurs.
- Helps in finding the exceptions being swallowed.
- You can add your own exception.

Presenter: Harsh Wardhan, Mindfire Solutions
Question and
Answer

Presenter: Harsh Wardhan, Mindfire Solutions
Thank you

Presenter: Harsh Wardhan, Mindfire Solutions

More Related Content

PPT
Exception Handling Mechanism in .NET CLR
PPTX
Presentation1
PPTX
Exception handling in asp.net
PPTX
Exception handling in ASP .NET
PPTX
What is Exception Handling?
PPT
Exception handling
PPTX
Exception handling
PPSX
Exception Handling
Exception Handling Mechanism in .NET CLR
Presentation1
Exception handling in asp.net
Exception handling in ASP .NET
What is Exception Handling?
Exception handling
Exception handling
Exception Handling

What's hot (20)

PPT
Exception handler
PDF
Exception handling
ODP
Exception Handling In Java
PPT
C# Exceptions Handling
PPT
Exception handling in java
PPTX
Exception handling in java
PPT
Exception handling
PDF
Exception handling
PPTX
Exception handling in c
PPTX
C++ ala
PPTX
7.error management and exception handling
PPTX
Exception handling in JAVA
PPTX
Exception Handling in Java
PDF
14 exception handling
PDF
javaexceptions
PDF
Best Practices in Exception Handling
PPT
Exception Handling Java
PPTX
Exception handling
PPTX
Exceptionhandling
PPTX
Java exception handling
Exception handler
Exception handling
Exception Handling In Java
C# Exceptions Handling
Exception handling in java
Exception handling in java
Exception handling
Exception handling
Exception handling in c
C++ ala
7.error management and exception handling
Exception handling in JAVA
Exception Handling in Java
14 exception handling
javaexceptions
Best Practices in Exception Handling
Exception Handling Java
Exception handling
Exceptionhandling
Java exception handling
Ad

Similar to Understanding Exception Handling in .Net (20)

PPT
06 exceptions
PPSX
Exception hierarchy
PPTX
Module 4.pptxModule 4.pptxModuModule 4.pptxModule 4.pptxle 4.pptx
PPTX
Exception handling
PPT
Exception
PPT
Exception handling
PPT
Java: Exception
PPT
Exceptionhandling
PPTX
Interface andexceptions
PPTX
PPTX
UNIT III 2021R.pptx
PPTX
UNIT III 2021R.pptx
PDF
Programming in Java Unit 1 lesson Notes for Java
PDF
CS3391 -OOP -UNIT – III NOTES FINAL.pdf
PDF
DEFCON 21: EDS: Exploitation Detection System WP
PPTX
Java Exceptions and Exception Handling
PPTX
Lecture 1 Try Throw Catch.pptx
PPTX
Exceptions overview
PPTX
8 Exception Handling
06 exceptions
Exception hierarchy
Module 4.pptxModule 4.pptxModuModule 4.pptxModule 4.pptxle 4.pptx
Exception handling
Exception
Exception handling
Java: Exception
Exceptionhandling
Interface andexceptions
UNIT III 2021R.pptx
UNIT III 2021R.pptx
Programming in Java Unit 1 lesson Notes for Java
CS3391 -OOP -UNIT – III NOTES FINAL.pdf
DEFCON 21: EDS: Exploitation Detection System WP
Java Exceptions and Exception Handling
Lecture 1 Try Throw Catch.pptx
Exceptions overview
8 Exception Handling
Ad

More from Mindfire Solutions (20)

PDF
Physician Search and Review
PDF
diet management app
PDF
Business Technology Solution
PDF
Remote Health Monitoring
PDF
Influencer Marketing Solution
PPT
High Availability of Azure Applications
PPTX
IOT Hands On
PPTX
Glimpse of Loops Vs Set
ODP
Oracle Sql Developer-Getting Started
PPT
Adaptive Layout In iOS 8
PPT
Introduction to Auto-layout : iOS/Mac
PPT
LINQPad - utility Tool
PPT
Get started with watch kit development
PPTX
Swift vs Objective-C
ODP
Material Design in Android
ODP
Introduction to OData
PPT
Ext js Part 2- MVC
PPT
ExtJs Basic Part-1
PPT
Spring Security Introduction
Physician Search and Review
diet management app
Business Technology Solution
Remote Health Monitoring
Influencer Marketing Solution
High Availability of Azure Applications
IOT Hands On
Glimpse of Loops Vs Set
Oracle Sql Developer-Getting Started
Adaptive Layout In iOS 8
Introduction to Auto-layout : iOS/Mac
LINQPad - utility Tool
Get started with watch kit development
Swift vs Objective-C
Material Design in Android
Introduction to OData
Ext js Part 2- MVC
ExtJs Basic Part-1
Spring Security Introduction

Recently uploaded (20)

PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
cuic standard and advanced reporting.pdf
PDF
Approach and Philosophy of On baking technology
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Network Security Unit 5.pdf for BCA BBA.
PPT
Teaching material agriculture food technology
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Machine learning based COVID-19 study performance prediction
PDF
Electronic commerce courselecture one. Pdf
20250228 LYD VKU AI Blended-Learning.pptx
Big Data Technologies - Introduction.pptx
Programs and apps: productivity, graphics, security and other tools
Per capita expenditure prediction using model stacking based on satellite ima...
The Rise and Fall of 3GPP – Time for a Sabbatical?
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Reach Out and Touch Someone: Haptics and Empathic Computing
Empathic Computing: Creating Shared Understanding
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
cuic standard and advanced reporting.pdf
Approach and Philosophy of On baking technology
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
MIND Revenue Release Quarter 2 2025 Press Release
Spectral efficient network and resource selection model in 5G networks
Network Security Unit 5.pdf for BCA BBA.
Teaching material agriculture food technology
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Machine learning based COVID-19 study performance prediction
Electronic commerce courselecture one. Pdf

Understanding Exception Handling in .Net

  • 1. Understanding Exception Handling in .net. Presenter: Harsh Wardhan, Mindfire Solutions Date: 29/10/2013
  • 2. Agenda - What is Exception? - The try, the catch & the finally. - A look into System.Exception class. - Throwing Exception. - Defining your own exception class. - Best practices. - Unhandled Exceptions. - Debugging Exceptions. Presenter: Harsh Wardhan, Mindfire Solutions
  • 3. What is Exception? - When a member fails to complete the task it is supposed to perform as indicated by its name, it is called an exception. Presenter: Harsh Wardhan, Mindfire Solutions
  • 4. The Try - A Try block contains code that require: - Common clean up operations. - Exception recovery operations. - A code that might potentially throw an exception. - A Try block should have at least one catch or finally block associated with it. - How much code should be added in a single try block? Presenter: Harsh Wardhan, Mindfire Solutions
  • 5. The Catch - Contains code to execute in response to an exception. - A try block can have multiple catch blocks. - The expression after Catch keyword is catch type. - CLR searches the catch type from top to bottom. Presenter: Harsh Wardhan, Mindfire Solutions
  • 6. The Catch - What to do in a catch block? – Re-throw same exception. – Throw a different exception with richer exception information to code higher up in the call stack. – Let the thread fall out of the bottom of the catch block. Presenter: Harsh Wardhan, Mindfire Solutions
  • 7. The Finally - It contains code that's guranteed to execute. - Mostly contains clean up operations required by actions taken in try block. - Code in catch and finally blocks should be short and should have likelihood of succeeding without itself throwing an exception. Presenter: Harsh Wardhan, Mindfire Solutions
  • 8. A look into System.Exception - Base class for all exceptions. - All user and system defined exceptions derive from System.Exception class. - Important Properties of System.Exception: - Message - Data - Source - Stack-trace - Target Site - Help link - Inner exception Presenter: Harsh Wardhan, Mindfire Solutions
  • 9. Throwing Exception - Points to consider while throwing an exception: – What exception derived type you need to throw. – What string message are you going to pass to the exception type's constructor. Presenter: Harsh Wardhan, Mindfire Solutions
  • 10. Defining your own Exception class - Create your exception class when the error is tightly bound to the class issuing the error. - Your exception class should be shallow and wide. Presenter: Harsh Wardhan, Mindfire Solutions
  • 11. Defining your own Exception class - Best practices for creating Exception class: – Should derive from ApplicationException – Marked with <System.Seralizable> attribute – Defines a default constructor – Defines a constructor that sets inherited message property. – Defines a constructor to handle inner exception. – Defines a constructor for serialization of your type. Presenter: Harsh Wardhan, Mindfire Solutions
  • 12. Best Practices - Use – – – finally block liberally. Use finally to clean up from any operation. Dispose objects to avoid resource leak. Examples in our language compiler: • Lock statement. • Using statement. • For each statement. Presenter: Harsh Wardhan, Mindfire Solutions
  • 13. Best Practices - Don't Catch everything. – When you catch an exception you are actually saying one of these things: • You expected this exeption. • You understand why it occurred. • You know how to deal with it. Presenter: Harsh Wardhan, Mindfire Solutions
  • 14. Best Practices - NEVER leave an empty catch block. - Always try to catch specific exceptions. - Catch a System.Exception and execute some code inside catch block as long as you re-throw the exception. Presenter: Harsh Wardhan, Mindfire Solutions
  • 15. Unhandled Exceptions - If no catch block matches the thrown exception type, an unhandled exception occurs. - Usually information gets logged in Event Log. Presenter: Harsh Wardhan, Mindfire Solutions
  • 16. Debugging Exceptions - You can enable the exception to break the flow when it occurs. - Helps in finding the exceptions being swallowed. - You can add your own exception. Presenter: Harsh Wardhan, Mindfire Solutions
  • 17. Question and Answer Presenter: Harsh Wardhan, Mindfire Solutions
  • 18. Thank you Presenter: Harsh Wardhan, Mindfire Solutions