HardCode Posted August 18, 2004 Posted August 18, 2004 In VB6, I could do a Select Case on Err.Number to handle specific errors in specific ways. I cannot seem to find the .NET equivalent. I tried ... Catch e As Exception Label1.Text = e.HResult ... in an ASP page's VB.NET Page_Init, but I get an error saying that e.HResult is Protected. What is the .NET way of doing this? Quote
Administrators PlausiblyDamp Posted August 18, 2004 Administrators Posted August 18, 2004 Most exceptions do not have (or even need an error code) because the type of the exception is the error. Rather than catch Exception catch the specific type you are after try 'something catch ex as DivideByZeroException 'handle catch ex as OverFlowException 'handle end try this gives far more readable code than the vb6 style of error 13 or 6 ,or 57 occurred (means nothing without documentation). It may help if you post the code within the try block - or at least give the exceptions error message. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
HardCode Posted August 18, 2004 Author Posted August 18, 2004 The error is in the VB part of Page_Init in an ASP.NET page, on Response.Redirect(sPath) where sPath is a local variable. sPath gets its value from a stored procedure. sPath with never be "" because the function that the stored procedure call is in will concatenate "/main.aspx" to the end of sPath. A possible value of sPath is "test/main.aspx". Supposing that there is no value retrieved from the store procedure, the value of sPath will be only "/main.aspx" and this does not exist. Hence the error I would get is: Server Error in '/' Application. -------------------------------------------------------------------------------- The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly. Requested Url: /xxxxxxxx/main.aspx -------------------------------------------------------------------------------- Version Information: Microsoft .NET Framework Version:2.0.40607.16; ASP.NET Version:2.0.40607.16 Now, you may suggest that I do something like If sPath = "/main.apsx" then don't redirect. However, there may be a value in the DB to return "test/main.aspx.", but if the subfolder "test/" doesn't exist, I will get the same error. So, I would like to trap this error in the redirect and then redirect to a definite page (like Response.Redirect("notfound.aspx"). I did notice that the URL of the error page is not the original page, rather the page I am trying to call (test/main.aspx). Where then should I handle the error? Will the calling page handle it? I am guessing the calling page CANNOT handle it because this: Try ' Do code here Response.Redirect("PageThatIsntOnServer.aspx") Catch e As Exception Label1.Text = e.Message Finally oConn.Close End Try ... will NOT stay on the calling page and change Label1.Text = e.Message. Quote
Administrators PlausiblyDamp Posted August 19, 2004 Administrators Posted August 19, 2004 Redirecting to a page that doesn't exist will not generate an exception - you just get a standard 404 error and the default error page. It may be easier to check the return value of sPath and if it equals "" then display the error message. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
HardCode Posted August 20, 2004 Author Posted August 20, 2004 Hmmm, I can use the FileSystemObject in ASP.NET right? If I can, then before the Redirect, I can just check if the file exists. If yes then Redirect. If not, then go to MyNotFoundPage.aspx. Think this will work? Quote
Administrators PlausiblyDamp Posted August 20, 2004 Administrators Posted August 20, 2004 An easier way would be to put code in the Application_Error event in the Global.asax file. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Arch4ngel Posted August 20, 2004 Posted August 20, 2004 I must agree with PD. Error handling is not as before. They created exception which I found really more useful. That way you can extract the exact error message and it's much more useful to understand fast. DivideByZeroException (happen when... well... need more ?). And must I mention that all exception derive from Exception (which means Oriented Object) ? And 404 error.... 403... 402... they are not "application" error. They are HTTP return code to say to the browser that something wrong happened or that the page is transfered (312 if I remember ?). So trying to catch 404 error or else won't be useful. If you want to verify if a file exists... don't use FileSystemObject is like a legacy command. Look at System.IO.File.Exists(path). it will be more .NET way. What I want to confirm (that I learned in school) is... does the TryCatchFinally block give a hit to the CPU And/Or the RAM ? Quote "If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown "Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me "A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend. C# TO VB TRANSLATOR
Administrators PlausiblyDamp Posted August 20, 2004 Administrators Posted August 20, 2004 Bit off topic but the way try ... catch ... finally is designed is that for a normal (i.e. no exception thrown) execution then the overheads are minimal. Exceptions being thrown / caught however do incur a noticable performance hit - then again if things are going astray performance (especially for a non-normal condition) is probably a secondary concern. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Arch4ngel Posted August 20, 2004 Posted August 20, 2004 Thanks for the off-topic. Quote "If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown "Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me "A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend. C# TO VB TRANSLATOR
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.