0
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...
What is Exception?
- When a member fails to complete the task it is
supposed to perform as indicated by its name, it is
ca...
The Try
- A Try block contains code that require:
- Common clean up operations.
- Exception recovery operations.
- A code ...
The Catch
- Contains code to execute in response to an
exception.
- A try block can have multiple catch blocks.
- The expr...
The Catch
- What to do in a catch block?
– Re-throw same exception.
– Throw a different exception with richer
exception in...
The Finally
- It contains code that's guranteed to execute.
- Mostly contains clean up operations required by
actions take...
A look into System.Exception
- Base class for all exceptions.
- All user and system defined exceptions derive from
System....
Throwing Exception
- Points to consider while throwing an exception:
– What exception derived type you need to
throw.
– Wh...
Defining your own Exception
class
- Create your exception class when the error is
tightly bound to the class issuing the e...
Defining your own Exception
class
- Best practices for creating Exception class:
– Should derive from ApplicationException...
Best Practices
- Use
–
–
–

finally block liberally.
Use finally to clean up from any operation.
Dispose objects to avoid ...
Best Practices
- Don't Catch everything.
– When you catch an exception you are
actually saying one of these things:
• You ...
Best Practices
- NEVER leave an empty catch block.
- Always try to catch specific exceptions.
- Catch a System.Exception a...
Unhandled Exceptions
- If no catch block matches the thrown exception
type, an unhandled exception occurs.
- Usually infor...
Debugging Exceptions
- You can enable the exception to break the flow
when it occurs.
- Helps in finding the exceptions be...
Question and
Answer

Presenter: Harsh Wardhan, Mindfire Solutions
Thank you

Presenter: Harsh Wardhan, Mindfire Solutions
Upcoming SlideShare
Loading in...5
×

Understanding Exception Handling in .Net

203

Published on

This seminar will make you familiar with the exception handling concepts in .Net. Anyone who has been working on any of the .net languages or has just started can join in to learn more here.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
203
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
16
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Understanding Exception Handling in .Net"

  1. 1. Understanding Exception Handling in .net. Presenter: Harsh Wardhan, Mindfire Solutions Date: 29/10/2013
  2. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 17. Question and Answer Presenter: Harsh Wardhan, Mindfire Solutions
  18. 18. Thank you Presenter: Harsh Wardhan, Mindfire Solutions
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×