What I got:
SecureBasePage.vb(to be inherited by pages that require to login)Default.aspxwhich inheritsSecureBasePage
What should be the case: client requests Default.aspx but gets redirected to login because he isn’t logged in.
SecureBasePage.vb has this constructor
Public Sub ()
If userIsAuthenticated Then
Else
Throw New LoginException()
End If
End Sub
This results in a exception LoginException. Now I want to let it being catched by a somehow global exception handler which kicks the client to login.aspx.
I know there is a function Application_Error() in Global.asax but in my opinion this isn’t an exception handler as it only takes the Server.GetLastError() and doesn’t catch the exception. So more likely this function is to allow a developer to send an email about the unhandled exception. I’d call this a ‘global unhandled exception handler’ instead of an ‘global exception handler‘.
Is there anything I can kinda try-catch to catch the constructors exception throwing?
Since all pages requiring login will inherit from the
SecureBasePageclass, it would probably make the most sense to implement your logic there. Just override one of the page methods likeOnLoadorOnPreLoadto handle the redirection.I wouldn’t advise depending on the
global.asaxto handle the errors, so your base class is probably the most appropriate place.EDIT
As a general rule of thumb, you shouldn’t use exceptions to control the flow of logic. Unless the exception itself contains useful logging information, it’s probably more costly than beneficial, especially for logic that’s going to be used on nearly every request.
This article offers some useful advice on the topic: