Exception handling is an in built mechanism in .NET framework to detect and handle run time errors. The .NET framework contains lots of standard exceptions. The exceptions are anomalies that occur during the execution of a program. They can be because of user, logic or system errors. If a user (programmer) do not provide a mechanism to handle these anomalies, the .NET run timeenvironment provide a default mechanism, which terminates the program execution.
VB.NET provides three keywords try, catch and finally to do exception handling. The try encloses the statements that might throw an exception whereas catch handles an exception if one exists. The finally can be used for doing any clean up process.
In VB.NET, exceptions are nothing but objects of the typeException. The Exception is the ultimate base class for any exceptions in VB.NET. The VB.NET itself provides couple of standard exceptions. Or even the user can create their own exception classes, provided that this should inherit from either Exception class or one of the standard derived classes of Exception class like DivideByZeroExcpetion ot ArgumentException etc.
Visual Basicsupports exception handling (errors) both structured and unstructured. Error handling formal and can establish a plan for possible errors, and so prevent them from interfering with the purported goals of the application. If an exception in a method that is not prepared to control, the exception is propagated back to the calling method or the above method. If the above method also has no exceptionhandler, the exception is propagated back to the caller of the method, and so on. The search for a driver to continue the call stack, which is the number of procedures that are called within the application. If it does not find a driver for the exception, display an error message and the application ends.
Sentences try – catch - finally
Undoubtedly one of the great innovations of VisualBasic. NET is the Try - Catch.
The mechanism Try - Catch serves to trap errors, as does On Error Go to, Resume and Resume Next, but structurally and conceptually different.
With the mechanism Try - Catch is possible to write structured error handlers to offer an effective way to solve the run-time errors.
The mechanisms leading to Try - Catch continue in force, including a new methodcalled Err.GetException, you get the exception that occurred when generating the error.
What is a runtime error?, Is but an unexpected error which a program in Visual Basic. NET can not be recovered, not able to complete an instruction then Visual Basic. NET has no specific instructions to be executed in these cases when generating the error.
To deal with errors in Visual Basic runtimehas this new mechanism Try - Catch to handle errors, as this error trapping mechanism defining the actions to take when you identify the error.
The way to use this mechanism is:
Try - Case (s) that can generate a runtime error
Catch - (The error is generated) - Case (s) that define the actions to follow as it generated the error
Finally - Case (s) that restore the conditionsbefore the error generated.
End Try - Finally The keyword is optional.
To test more than one error condition at runtime When using the Catch statement:
Catch When Err.Number = 13
'There was an error of type data type mismatch
Catch When Err.Number = 6
'There was an error in the rate control, overflow
Finally, this mechanism aswell as control statements have a way to either block out the Try or Catch through the use of the Exit try, but if it contains a sentence Finally the code in this if running, only applies to Try and Catch Finally because the objective is precisely the run no matter what its content.
'Sentence (s) that can generate a runtime error
'Under certain conditions, try Exit