+ All Categories
Home > Documents > Topik 4.4 Exception Handling

Topik 4.4 Exception Handling

Date post: 04-Apr-2018
Category:
Upload: pabburahati
View: 217 times
Download: 0 times
Share this document with a friend

of 28

Transcript
  • 7/30/2019 Topik 4.4 Exception Handling

    1/28

    F5227VISUAL BASIC .NET PROGRAMMING

    Topic 4.4 :

    Understand the concepts of Exception

    Handling

  • 7/30/2019 Topik 4.4 Exception Handling

    2/28

    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 applicationthat uses an XML Web Service and manipulate data in

    database by using Microsoft ADO.NET.

  • 7/30/2019 Topik 4.4 Exception Handling

    3/28

    Course Learning Outcome:

    Topic 4.0

    1. Apply object oriented programming techniques to

    create classes, add methods and add properties. (CLO3)

  • 7/30/2019 Topik 4.4 Exception Handling

    4/28

    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 ofException Handling

  • 7/30/2019 Topik 4.4 Exception Handling

    5/28

    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.

  • 7/30/2019 Topik 4.4 Exception Handling

    6/28

    Types of Errors

    Syntax

    Run-time

    Logic

  • 7/30/2019 Topik 4.4 Exception Handling

    7/28

    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 ofByVal.

    It could also be a bad expression such as 524+

    + 62.55.

    It could be a "grammar" error

  • 7/30/2019 Topik 4.4 Exception Handling

    8/28

    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 executedthe application in your machine, everything

    was fine. This is a type ofrun-time error.

  • 7/30/2019 Topik 4.4 Exception Handling

    9/28

    Run-time Error

    Run-time errors are mostly easy to fix because

    you will know what problem is occurring and

    why.

  • 7/30/2019 Topik 4.4 Exception Handling

    10/28

    Logic Error

    These are errors that don't fit in any of the abovecategories.

    They could be caused by the user misusing your

    application, a problem with the computer on whichthe 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.

  • 7/30/2019 Topik 4.4 Exception Handling

    11/28

    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.

  • 7/30/2019 Topik 4.4 Exception Handling

    12/28

    Types of Exception Handling

    Exception can be handled by two ways in

    VB.NET .

    Structured exception handling (SEH).

    Unstructured exception handling (unSEH).

  • 7/30/2019 Topik 4.4 Exception Handling

    13/28

    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.

  • 7/30/2019 Topik 4.4 Exception Handling

    14/28

    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.

  • 7/30/2019 Topik 4.4 Exception Handling

    15/28

    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.

  • 7/30/2019 Topik 4.4 Exception Handling

    16/28

    Syntax

    Try

    ' Statement which can cause an exception.

    Catch x As Type

    ' Statements for handling the exception

    Finally

    End Try 'Any cleanup code

  • 7/30/2019 Topik 4.4 Exception Handling

    17/28

    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.

  • 7/30/2019 Topik 4.4 Exception Handling

    18/28

    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.

  • 7/30/2019 Topik 4.4 Exception Handling

    19/28

    Try

    ' Code to Execute In Case Everything is Alright

    Catch' If Something Bad happened, deal with it here

    End Try

  • 7/30/2019 Topik 4.4 Exception Handling

    20/28

    Example

    Module Module1Sub 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

  • 7/30/2019 Topik 4.4 Exception Handling

    21/28

    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.

  • 7/30/2019 Topik 4.4 Exception Handling

    22/28

    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.

  • 7/30/2019 Topik 4.4 Exception Handling

    23/28

    Syntax

    Try

    Catch ex As Exception

    End Try

    By default, an exception is first of type Exception

  • 7/30/2019 Topik 4.4 Exception Handling

    24/28

    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.

  • 7/30/2019 Topik 4.4 Exception Handling

    25/28

    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.

  • 7/30/2019 Topik 4.4 Exception Handling

    26/28

    Public Class CustomException

    Inherits ApplicationException

    Public Sub New(ByVal message As String)MyBase.New(message)

    End Sub

    End Class

  • 7/30/2019 Topik 4.4 Exception Handling

    27/28

    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

    TryNumber = Me.TextBox1.Text

    Twice = Number * 2

    Me.TextBox2.Text = Twice

    Catch ex As Exception

    MsgBox(ex.Message)

    End Try

    End Sub

  • 7/30/2019 Topik 4.4 Exception Handling

    28/28

    Output Message


Recommended