Forums: Back End:

 

VB.NET(ASP.NET) Exception handling

first
 

JimmyTheGent VB.NET(ASP.NET) Exception handling



Hey Folks

I want to use a function in the app_code folder to determine how an exception is handled. I have created the function already in a file called logerror.vb in the desktopKB namespace. The function is called checkexception and takes an exception as input.

On my code behind page I am writing a catch block for example:

Catch ex as SqlException when xxxx(ex)

I want the xxxx to refer to my function. I know it would be easy to just put the function on the code behind but I will be reusing the code, so it makes sense to have it in app_code

If i were accessing this function normally it would be pretty simple.
I could just use

dim exHandler as new desktopKB.logerror
exHandler.checkexception(ex)

However I am unsure how to use it on the same line as the catch. The syntax noted above is perfect when the function is on code behind.

 

jamiec

can you not do:

Catch ex as SqlException 
if myCode.checkexception(ex) then
throw ex
end if
end catch

 

JimmyTheGent

Am I right in thinking that only one of the catch clauses is used? For example if i have 4 catches, if 1 matches, then it goes to finally?

If so then I cannot do what you suggested because if it returns false i need it to goto the next catch.

If I am wrong, which is more than possible since I am relatively new to .NET then I feel kind of stupid for even asking the question smile

As always though jamiec, you are very helpful and wise not worthy

 

jamiec

yeah only 1 will be hit (the first that gets a match) - but there is no extension to the catch construct (like your original post) that allows a boolean check.

Take a step backwards - what are you trying to achieve? Have you looked at the MS Exception Management Application Block (part of their patterns & practices stuff if you want to search for it)? Its very very useful for doing all kinds of giggery-pokery with exceptions.



 

JimmyTheGent

C# does not have the 'When' Parameter but VB.NET does.

If for example I had what is below this would work. My only real problem was puting the function in app_code.

Like you said though, it may be worth revisiting my plan for exception handling as there may be a better way for doing it....

I will maybe see if I can restructure it so it uses 1 catch which refers to a class in app_code, that class can then go through all the different possibilities and could refer to different functions and subs in that class if need be. probably a better way of doing it anyway, keeping it all in one code file.


....

catch ex as exception when myfunction(ex)
....

finally
....

end sub


function myfunction(byval ex as exception)
.....
end function


 

jamiec

wait, that when thing actually works in VB.NET? mad.

 

JimmyTheGent

Yea it does. The ONE thing vb.net has over c#

 

jamiec

nahh. it has the My namespace too wink

 
first
 

Forums: Back End: VB.NET(ASP.NET) Exception handling

 
New Post
 
You must be logged in to post