-
Catch all errors
Is there a way to catch ALL errors of the program?
Purpose: The idea is to catch all the errors of the program, load errorform which basically just says "A Error Was Detected" (Which I have already designed). Then send a web request to a formmail.php file with the said error (which i have already done).
In short: I need a way to catch ALL errors, and get the error string that it gave.
Thanks!
-
Re: Catch all errors
well you need to use Try Catch End Try
Code:
Try
your code
Catch ex as exception
msgbox(ex.tostring)
End Try
Note this is very bad programming practice and you should be using more specific exception types for the error you expect i.e. if you are doing an httpRequest you would use
Catch ex as HttpRequestException
this provides a more specific error that is easier to trace than a general exception. you can use multiple Catches with them getting more general and finally use Catch ex as Exception.
In my own exception handling routines i tend to do the eqivelant with the msgbox as
MsgBox("Name of the method where the error is generated" & ex.tostring)
obviously you wouldn't be using msgbox but the idea is there
On another note for what you are looking at then you would enclose any sections of code where an error could be generated with its own Try Catch routine, you cant encapsulate the entire program in a single Try Catch End Catch routine, again it would be bad practice if you could when you can narrow down an error precisely with this method.
Final note if you are developing code it is good practice not to catch errors and let the debugger find them but yes for a final project you should do error trapping
-
Re: Catch all errors
Thank you sir, I will end up doing that
-
Re: Catch all errors
thought an example of multiple catches would help
Code:
Try
some code that could produce a http error or an unknown error
Catch ex as HttpResponseException
handle this error state what it is
Catch ex as Exception
this is an unknown error if it gets to here
Finally
finally is optional but you use it to close streams etc so they aren't left open when the routine crashes. VB.NET does it automatically but it is good practice
End Try
safe code that wont produce an error
Finally is executed regardless of any errors but is optional and not necessary, i use it as it makes sense to my way of thinking.