.comment-link {margin-left:.6em;}

Friday, December 09, 2005

 

Filtering Exceptions

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 .

3-
Sub A
Try
B()
Catch when Filter1() = True
DoSomething()
End Try
End sub

Function Filter1() as boolean
return True
End function

Sub B
Try
DoWork()
Finally
FinalizeMyWork()
End Try
End sub

Sub DoWork()
throw new exception()
End sub

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
Handler
5- this is the trick . Filter1 is executed
6- B's Finally
7- DoSomething


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 .


Comments: Post a Comment

Links to this post:

Create a Link



<< Home

This page is powered by Blogger. Isn't yours?