understanding exception handling in .net

18
Understanding Exception Handling in .net. Presenter: Harsh Wardhan, Mindfire Solutions Date: 29/10/2013

Upload: mindfire-solutions

Post on 11-May-2015

187 views

Category:

Technology


0 download

DESCRIPTION

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.

TRANSCRIPT

Page 1: Understanding Exception Handling in .Net

UnderstandingException Handling in .net.

Presenter: Harsh Wardhan, Mindfire SolutionsDate: 29/10/2013

Page 2: Understanding Exception Handling in .Net

Presenter: Harsh Wardhan, Mindfire Solutions

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.

Page 3: Understanding Exception Handling in .Net

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.

Page 4: Understanding Exception Handling in .Net

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?

Page 5: Understanding Exception Handling in .Net

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.

Page 6: Understanding Exception Handling in .Net

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.

Page 7: Understanding Exception Handling in .Net

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.

Page 8: Understanding Exception Handling in .Net

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

Page 9: Understanding Exception Handling in .Net

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.

Page 10: Understanding Exception Handling in .Net

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.

Page 11: Understanding Exception Handling in .Net

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.

Page 12: Understanding Exception Handling in .Net

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.

Page 13: Understanding Exception Handling in .Net

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.

Page 14: Understanding Exception Handling in .Net

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.

Page 15: Understanding Exception Handling in .Net

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.

Page 16: Understanding Exception Handling in .Net

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.

Page 17: Understanding Exception Handling in .Net

Presenter: Harsh Wardhan, Mindfire Solutions

Question and Answer

Page 18: Understanding Exception Handling in .Net

Presenter: Harsh Wardhan, Mindfire Solutions

Thank you