There is some trouble to debug net code that is called by the system in sharepoint. For example owstimer can call custom expiration formula class asynchronously for the user. So, the ordinary method to attach visual studio debugger do not help. The solution is very simple – insert Debug.Assert(1==2, “some message”) statement in your code. When the exeption occures, press retry and choose visual studio debugger. Cetainly the custom class must contain debug information and do not be optimized.