UnderstandingException Handling in .net.
Presenter: Harsh Wardhan, Mindfire SolutionsDate: 29/10/2013
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.
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
Top Related