Escolar Documentos
Profissional Documentos
Cultura Documentos
NET PROGRAMMING
Topic 4.0
Topic 4.1 : Use Classes, Object and Methods Topic 4.2 : Apply Inheritance And Polymorphism Topic 4.3 : Construct Program Using String Methods Topic 4.4 : Understand the concepts of Exception Handling
Types of Errors
Syntax Run-time Logic
Syntax Error
A syntax error comes from your mistyping a word or forming a bad expression in your code. It could be that you misspelled a keyword such as ByVel instead of ByVal. It could also be a bad expression such as 524+ + 62.55. It could be a "grammar" error
Run-time Error
For example, in your code, you indicate that a picture would be loaded and displayed to the user but you forget to ship the picture or the directory of the picture indicated in your code becomes different when a user opens your application. In this case, when you compiled and executed the application in your machine, everything was fine. This is a type of run-time error.
Run-time Error
Run-time errors are mostly easy to fix because you will know what problem is occurring and why.
Logic Error
These are errors that don't fit in any of the above categories. They could be caused by the user misusing your application, a problem with the computer on which the application is running while the same application is working fine in another computer. Because logic errors can be vague, they can also be difficult (even, to the extreme, impossible) to fix.
Exception Handling
Exception handling is a technique of handling a situation which is occur at the run time when program abort without execution and the cause of that situation is called exception.
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.
Syntax
Try ' Statement which can cause an exception. Catch x As Type ' Statements for handling the exception Finally End Try 'Any cleanup code
If any exception occurs inside the try block, the control transfers to the appropriate catch block and later to the finally block. But in VB.NET, both catch and finally blocks are optional. The try block can exist either with one or more catch blocks or a finally block or with both catch and finally blocks.
If there is no exception occurred inside the try block, the control directly transfers to finally block. We can say that the statements inside the finally block is executed always. Note that it is an error to transfer control out of a finally block by using break, continue, return or goto.
Try ' Code to Execute In Case Everything is Alright Catch ' If Something Bad happened, deal with it here End Try
Example
Module Module1 Sub Main() Dim a = 0, b = 1, c As Integer Try c=b/a Console.WriteLine("C is " & c) Catch e As Exception Console.WriteLine(e) Console.WriteLine("0 can't be divided by any number") Console.ReadLine() End Try End Sub End Module
Output
In this code a number is divided by zero which result is infinity. This error is thrown by Try Block and Catch block shows the exception after catching it.
Exception class
To support exception handling, the .NET Framework provides the Exception class. Once the compiler encounters an error, the Exception class allows you to identify the type of error and take appropriate action.
Syntax
Try
Exception
There are two types of exceptions:
exceptions generated by an executing program exceptions generated by the common language runtime.
System.Exception is the base class for all exceptions in VB.NET. The user-defined exception classes must inherit from either Exception class or one of its standard derived classes.
Exception class
If you declare the exception as an Exception type, this class will identify the error. One of the properties of the Exception class is called Message. This property contains a string that describes the type of error that occurred. You can then use this message to display an error message if you want.
Public Class CustomException Inherits ApplicationException Public Sub New(ByVal message As String) MyBase.New(message) End Sub End Class
Example of Exception
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim Number As Double Dim Twice As Double Try Number = Me.TextBox1.Text Twice = Number * 2 Me.TextBox2.Text = Twice Catch ex As Exception MsgBox(ex.Message) End Try End Sub
Output Message