Hello Friend’s Here we will learn how to do error handling in vb.net. When develop the any software we write the code for develop software but there sometime occur the errors, so it is very important to handle the errors. There are so many ways to handle the errors and there different ways of handling the error in different platform or language. Here we will see vb.net error handling with some examples. This post is very useful for beginner in software development to handle the errors. Now let’s start to see error handling.
Error:
Error means bug that means when you compile or run your program, abnormally situation may raise which is known as Error. There are different types of Error. First one is Syntax Error, second is Logic Error and third one is Runtime Error.
Syntax Error :
The error raise while misspelled keywords or variables is called syntax error. Compiler is easy to catch these errors while you are coding or after compile of the program.
Logic Error :
Due to the mistake or flaw present in the sequence of statements, code does not act as expected. This type of error is called Logic Error.
Runtime Error :
This error is occurring when code is executing it is called Runtime error. It occurs if you do not handling an unexpected error.
Exception:
Exceptions are run-time errors, which are raised when program is running. There are two ways to handle errors that occur at runtime. First one is Unstructured exception handling and second one is Structured exception handling.Unstructured Exception Handling
Unstructured Exception Handling is implemented with On Error Go To Statement, that is placed at the beginning of a code block to handle all possible exceptions that occurs during the execution of the code. If any exception occurs is fatal then your program will stop.
Syntax of Unstructured exception handling
On Error { Go To [ Line | Label | 0 | -1 ] | Resume Next | Line }
Go To lable | line – The arguments is any line label or line number. If an exception occurs, program exception goes to the given location.
Go To 0 – Disables enabled exception handler in the current procedure and reset it to nothing. It clears the Error object.
Go to 1 – same as Go To 0.
Resume Next – It specifiers that when an exception occurs, execution skips over the statement that cause the problem and goes to the statement immediately following and continues from that point.
Example
Private Sub Btn_Click()
Dim n1,n2 As Integer
On Error Go To err_msg
n1 = 14
n2 = 5
Txtresult.Text = n1 mod n2
Exit Sub
Err_msg :
MsgBox(“Division by zero error”)
End Sub
Here, if Exception occurs then the exception is continued from the label specified with the On Error Go To Statement.
When an error occurs, the Err object contains information about the error which helps you to determine whether you can attempt to fix the occur or ignore the error. The Err object also has methods that allow you to raise errors or clear the state of the Err object. These properties of Err object is Number, Description, Erl, HelpFile, source, HelpContext and the methods are Clear(), Raise().
When an error occurs, the Err object contains information about the error which helps you to determine whether you can attempt to fix the occur or ignore the error. The Err object also has methods that allow you to raise errors or clear the state of the Err object. These properties of Err object is Number, Description, Erl, HelpFile, source, HelpContext and the methods are Clear(), Raise().
Structured Exception Handling
A structured exception handling implemented with the Try…Catch…Finally statement, which is divided into a Try block, optional Catch blocks and an optional Finally block. The Try block contains code but when exceptions can occur; the Catch block contains code to handle the exceptions that occur. IF an exception occurs in the Try block the code throws the exception, so it can be caught and handled by the appropriate Catch statement. After the rest of the statement finishes, execution is always passed to the Finally block.
Syntax
A structured exception handling implemented with the Try…Catch…Finally statement, which is divided into a Try block, optional Catch blocks and an optional Finally block. The Try block contains code but when exceptions can occur; the Catch block contains code to handle the exceptions that occur. IF an exception occurs in the Try block the code throws the exception, so it can be caught and handled by the appropriate Catch statement. After the rest of the statement finishes, execution is always passed to the Finally block.
Try
[Try statements]
[Catch [exception1 [As type 1]] [when expression 1]
Catch statement1
[Catch [exception1 [As type2]] [when expression 2]
Catch statement2
[Exit Try]]
…………
[Catch [exception [As type N]] [when expression N]
Catch statement
[Exit Try]]
[Finally
[finally statement]]
End Try
Here type1,type2,….typeN in the Catch block are the Exception type which can be name of the Exception class that you want to handle.
Exceptions are based on the Exception classes. The base class in the hierarchy of the Exception classes is System.Exception. It has two derived classes first one is System.ApplicationException and second one is System.SystemException.
Exceptions are based on the Exception classes. The base class in the hierarchy of the Exception classes is System.Exception. It has two derived classes first one is System.ApplicationException and second one is System.SystemException.
Exist Try:
It is optional keyword and it breaks out of the Try..Catch..Finally structure. The Finally statement will still be executed. It is not allowed in Finally blocks.
Example
It is optional keyword and it breaks out of the Try..Catch..Finally structure. The Finally statement will still be executed. It is not allowed in Finally blocks.
Private Sub BtnExample_Click()
Dim n1,n2,n3 As Integer
Try
n1 = 14
n2 = 0
n3 = n1 mod n2
MsgBox(“Result:” & n3)
Catch ex As Exception
MsgBox(“Run-time Error occur”)
End Try
End Sub
Here, Try block contains the code that contains the code that may raise the error and the Catch block contains the code that will handle the error. When the statement n3 = n1 mod n2 will be executed the error will be raised as n2 = 0. The control will pass to Catch block, There the exception object ex is created and it will handle the error.
By this above ways you can do Vb.net error handling or exception handling, you can also throw the exceptions anywhere and you can throw a custom exception using the throw statement and you can also caught exception using the Throw statement.
No comments:
Post a Comment