Você está na página 1de 28

F5227 VISUAL BASIC .

NET PROGRAMMING

Topic 4.4 : Understand the concepts of Exception Handling

Course Learning Outcome (CLO)


Upon completion of this course, students should be able to : 1. Create a simple VB.NET based application based on the Windows Application template. 2. Describe on essential terminology including memory, data types and graphical user interface. 3. Apply object oriented programming techniques to create classes, add methods and add properties. 4. create a simple VB.NET based Web forms application that uses an XML Web Service and manipulate data in database by using Microsoft ADO.NET.

Course Learning Outcome:


Topic 4.0
1. Apply object oriented programming techniques to create classes, add methods and add properties. (CLO3)

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

Error & Exception Handling


There will be problems with your code or your application. Some problems will come from you. Some problems will be caused by users. And some problems will be caused by neither you nor your users.

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.

Types of Exception Handling


Exception can be handled by two ways in VB.NET .
Structured exception handling (SEH). Unstructured exception handling (unSEH).

Unstructured exception handling (unSEH)


On Error GoTo On Error GoTo system provides a global variable named Err. Err allows you to identify the error and its description. Because an error depends on what caused it and why, the values of the Err variable also depend and are not always the same.

Structured exception handling (SEH)


SEH is based on two main keywords: Try and Catch. An exception handling section starts with the Try keyword and stops with the End Try statement. Between Try and End Try, there must by at least one Catch section.

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

Catch ex As Exception End Try


By default, an exception is first of type Exception

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

Você também pode gostar