Friday, December 09, 2005
Some languages like VB.NET but not C# supports Exception Filters .
Let's take these quick scenarios :
1- procedure A which has an exception handler is calling Proc B which
doesn't have an exception handler. Proc B Throw an exception . What happens
? Of course the runtime will search the stack for any catch until it hits A.
so A cacth the exception
2- same as before but B has an exception handler . What happens ? Of course
B catch the exception which might in turn voluntarily throw the exception .
Catch when Filter1() = True
Function Filter1() as boolean
throw new exception()
The question now is : what are the ordered steps of execution ?
1- A calls B
2- B calls DoWork()
3- DoWork throw an exception
4- CLR will search in the stack for a handler until it finds A's
5- this is the trick . Filter1 is executed
6- B's Finally
So here is the lesson : TAKE CARE , some code could run after the try and
before the Finally . In our case , from B's Perspective , DoWork() is
executed then (SHOULD) followed by FinalizeMyWork(). But in practical , NO the
filter is executed in between . For me it is like (not is) code injection .