Handle .NET exceptions within Classic ASP pages - exception

I am making MSSQL stored procedure CLR calls from ASP pages. When an exception occurs, it is logged and then rethrown. In this scenario I need to be able to handle the exception (if possible) in the ASP page. Note that I cannot move away from classic ASP in this instance; I am stuck within a legacy system for this project. Please let me know if you know of a way to handle the exceptions in classic ASP. I appreciate the help!
Thanks,
Tyler

The code language of classic ASP is VBScript, so you have to make do with the error handling capabilities of that language, by using the "On Error ..." construct. You need to decide on whether to make a general error handler or insert some specific error handling logic for the SQL calls.
These are your options for error handling:
On Error Goto 0 ' Turns off error trapping. This is most likely what you got now
On Error Resume Next ' In case of error, simply execute next statement. Not recommended !
On Error Goto <label> ' Go to the specified label on an Error.
If you use On Error Goto ..., The Err object will contain error information. This means you should be able to write somehting like:
On Error Goto errorHandler
' Your code here.
errorHandler:
' Handle the error somehow. Perhaps log it and redirect to a prettier error page.
Response.Write Err.Number
Response.Write Err.Description

Related

Access violation, NotEnemy is 0xBF800000

I use cocos2d-x to do the game. After running my program for a while, the variable "NotEnemy" will always report the "access violation" error. NotEnemy is the variable I set in EventListenerPhysicsContact, always using cocos2d-x There will be errors like the title, all I want to know when there is such a mistake, is there any solution, I hope someone can tell me, right, NotEnemy I am assigned this way, auto NotEnemy=contact.getShapeB( )->getBody()->getNode();
I tried setting it to nullptr after using NotEnemy, but it didn't work. Now it generates a new error "Exception at 0x76EE35D2 (located in TankBattle.exe): Microsoft C++ Exception: std::exception, in memory Location 0x00C5F548.
There is an unhandled exception at 0x7640E6EB (ucrtbase.dll) (in TankBattle.exe): A critical program exit has been requested. ", used to be an error about access permissions conflicts, but they are all problems with NotEnemy, and they all appear in the "NotEnemy->SetPosition(Vec2(166,14))" statement.

Invalid Qualifier when using System.IO.path in VBA

I'm extremely new to vba and feel like I have been falling down at the first hurdle all morning. I'm trying to get the path of my access file by doing the following
Sub getDirectoryPath()
Debug.Print (System.IO.path.GetFullPath())
End Sub
However I get an "Invalid Qualifier" error on System when I try to run it. I've tried adding the 'System' reference but then it says IO is not found. What am I doing wrong?
VBA environment has only access to COM (and COM visible) component.
So forget about importing usual .Net namespaces.
But some Wrappers exists : [https://technet.microsoft.com/en-us/magazine/2007.01.heyscriptingguy.aspx]
For instance this works :
DataList = CreateObject("System.Collections.ArrayList")
BTW, in order to parse file full name in VBA, you can use FileSystemObject.

Async exception not caught by debugger

I'm working on an MVC5 project in VS2013. I seem to be finding that most (but not all) of my exceptions are being ignored by the debugger and as a result I end up with the exception and stack trace simply being written to the browser, precluding any examination of the objects involved in the exception.
For instance - I deliberately code an exception to prove the point:
<Authorize(Roles:="IdentityAdmin")>
Public Async Function Import(model As RegisterViewModel) As Task(Of ActionResult)
Dim a As Object = "he"
Dim b As Integer = a
Clearly the last line will throw a 'type mismatch' exception which I think should result in the debugger halting execution, highlighting the error in the VS2013 UI to enable me to examine the various objects and determine the problem.
Instead I simply find myself with the browser detailing the exception and VS2013 unresponsive:
Server Error in '/' Application.
Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.
Exception Details: System.FormatException: Input string was not in a
correct format.
Source Error:
Line 291: If Db.Users.Find(acct.username) Is Nothing Then
Line 292: Dim a As Object = "he"
Line 293: Dim b As Integer = a
When I insert the same exception-generating code into a non-async part of the code the VS debugger does catch the exception - so I am guessing this is an issue with debugging async code. Is it really the case that the VS debugger can't catch these exceptions?
UPDATE
After further searching I came across a suggestion to disable 'Just My Code' and manually enable various types of exception. There was the expected hailstorm of First Chance Exceptions most of which I could tune out by disabling certain exceptions. But this DID 'fix' the behaviour described above. It seems that the debugger is regarding my child async threads as 'Not My Code'. Slightly baffled but I guess this could be an answer of sorts?
As per the update above, the problematic behaviour detailed in the question seems to be fixed by a combination of:
Turn off 'Just My Code' in the debugger options.
Enable certain classes of exceptions in Debug/Exceptions.. - in my case Common Language Runtime Exceptions and Managed Debugging Assistants seemed to ensure that the debugger caught all my exceptions (async or otherwise).
Disable thrown (but unremarkable) exceptions which cause most grief (wrong globalisation etc).
The last two stages were happened upon somewhat by trial and error and I suspect I should ultimately chase down most of these non-breaking exceptions when I clean the project up.
Disabling Just My Code should not make a difference in this case, enabling "Break when an exception is thrown" is what you want to make the debugger stop at the correct place.
The issue is that when you make the method Async, it runs in a background Task. The Task will catch any exceptions that occur in the code it is executing and re-throw that exception to whatever uses the result of the Task. So for example if you have the following MVC code
Async Function Index() As Task(Of ActionResult)
Dim n as Integer = Await Method1()
Return View()
End Function
Async Function Method1() As Task(Of Integer)
Dim a As Integer = 0
Dim b As Integer = 1 / b
Return b
End Function
The Task executing the code inside of Method1 will catch the exception, and throw it in Index since it is using the result of Method1, then the Task executing Index will catch that exception and re-throw it to the MVC framework code that is using the result of Index, and then the MVC framework handles the exception and displays the error page.
When Just My Code is enabled and methods are executing synchronously the debugger will stop when an exception propagates out of user code with the message that it was "user unhandled". In the case above if an exception propagates from Index to the MVC framework which is not your code, the debugger will stop when JMC is enabled. Since in the case of an asynchronous method the exception is not unhandled but caught by the Task, it does not cross out of user code so disabling Just My code will not make a difference.
The reverse of this is, if an exception occurs in your synchronous method and you have disabled Just My Code, the debugger would not stop anyway, because there is no notion of "User Unhandled" when JMC is disabled and the MVC framework will ultimately handle that exception.

Typo doesn't trigger error handling in Acess VBA

After recently adding some new VBA code that referred to a control, my code wouldn't run. On investigation, I found the problem was a typo in the code referring to the control: I'd typed it as Me.CheckYearEmd (with an m) rather than Me.CheckYearEnd.
This is now resolved and the code's working fine, but the weird thing is the error didn't trigger my error handling; it just didn't run. My error handling works on a very simple basis:
On Error GoTo ErrHandler
... 'Rest of sub
Exit Sub
ErrHandler:
DoCmd.SetWarnings True
MsgBox "The database has generated an error. Please contact the database administrator, quoting the following error message: '" & Err.Description & "'", vbCritical, "Database Error"
Is there something specific about referring to a control that doesn't exist (as I effectivley did above) that causes it to bypass error handling, and is there a way of catching these errors?
I can't recreate the situation you described. When I misspell a control name in the form's code, running Debug->Compile from the VB Editor's main menu triggers a compile error: "Method or data member not found".
However, if I don't run Debug->Compile and just try to run the form's procedure with the misspelled name, I still get that compile error.
Although I can't duplicate your situation, I'll offer a guess why the procedure's error handler doesn't catch it. The directive, On Error GoTo ErrHandler, traps run-time errors. But the misspelled control name triggers a compile error.

Extract c function with goto or break

On C project I currently work at there is much code that uses goto and break for error handling. Break and goto are usually found inside
if (error_occured) {
LOGGER_REPORT_ERROR();
goto cleanup;
}
I tried extracting code that includes chunks like this with Eclipse CDT refactoring but it did not work automatically.
This should be theoretically possible to do this automatically if additional error code variable is created in original function and extracted function returns error code somehow.
Is there an IDE or IDE plugin for automatic refactoring that supports such function extractions already?