vb .net tutorial - 11

Upload: mathes99994840202

Post on 30-May-2018

233 views

Category:

Documents


2 download

TRANSCRIPT

  • 8/14/2019 VB .net tutorial - 11

    1/30

    Exception Handling and Debugging

    NIIT Exception Handling and Debugging/Lesson 11/Slide 1 of 30

    Objectives

    In this lesson, you will learn to:

    Identify the mechanism to handle run time errors

    Differentiate between structured and unstructured exceptionhandling

    Handle exceptions

    Debug an application

  • 8/14/2019 VB .net tutorial - 11

    2/30

    Exception Handling and Debugging

    NIIT Exception Handling and Debugging/Lesson 11/Slide 2 of 30

    Problem Statement 11.D.1

    While executing the application for adding order details, if theserver returns an error or there is insufficient memory toexecute the application, an appropriate message should bedisplayed and the application should not terminate abruptly.

  • 8/14/2019 VB .net tutorial - 11

    3/30

    Exception Handling and Debugging

    NIIT Exception Handling and Debugging/Lesson 11/Slide 3 of 30

    Task List

    Identify the mechanism to handle errors.

    Write the code to handle errors.

    Execute the application to verify the results of exceptionhandling.

  • 8/14/2019 VB .net tutorial - 11

    4/30

    Exception Handling and Debugging

    NIIT Exception Handling and Debugging/Lesson 11/Slide 4 of 30

    Task 1: Identify the mechanism to handle errors.

    An exception is termed as an abnormal conditionencountered by an application during execution.

    Exception handling

    Is the process of providing an alternate path to beexecuted when the application is unable to execute inthe desired way.

  • 8/14/2019 VB .net tutorial - 11

    5/30

    Exception Handling and Debugging

    NIIT Exception Handling and Debugging/Lesson 11/Slide 5 of 30

    Task 1: Identify the mechanism to handle errors.(Contd.)

    Structured exception handling

    All the exceptions are derived from theSystem.Exception class.

    System.Exception

    System.ApplicationException

    System.Windows.Forms.AxHost.InvalidActiveXStateException

    System.Runtime.Remoting.MetadataServices.SUDSParserException

    System.IO.IsolatedStorage.IsolatedStorageException

    System.SystemException

    System.Runtime.Remoting.MetadataServices.SUDSGeneratorException

  • 8/14/2019 VB .net tutorial - 11

    6/30

    Exception Handling and Debugging

    NIIT Exception Handling and Debugging/Lesson 11/Slide 6 of 30

    Task 1: Identify the mechanism to handle errors.(Contd.)

    Structured exception handling (Contd.)

    Divides an application into blocks of code that havethe probability of raising an error.

    Uses the TryCatchFinally statement.

    Errors are filtered in the Catch block.

  • 8/14/2019 VB .net tutorial - 11

    7/30

    Exception Handling and Debugging

    NIIT Exception Handling and Debugging/Lesson 11/Slide 7 of 30

    Just a Minute

    In the TryCatchFinally statement, when is the codegiven in each of the blocks executed?

  • 8/14/2019 VB .net tutorial - 11

    8/30

    Exception Handling and Debugging

    NIIT Exception Handling and Debugging/Lesson 11/Slide 8 of 30

    Task 1: Identify the mechanism to handle errors.(Contd.)

    User-defined exceptions

    Can be created by deriving a class from theApplicationException class.

    Unstructured exception handling

    Is done by placing an On Error statement in a block of

    code. Uses the On Error GoTo statement to pass the control

    to a specific error handler when an error occurs.

    Uses the On Error Resume Next statement to passcontrol to the next executable statement when an erroroccurs.

    Uses the OnErrorGoTo0 to disable an error handler.

  • 8/14/2019 VB .net tutorial - 11

    9/30

    Exception Handling and Debugging

    NIIT Exception Handling and Debugging/Lesson 11/Slide 9 of 30

    Task 1: Identify the mechanism to handle errors.(Contd.)

    Result:

    Since unstructured exception handling code is difficult tomaintain, you will use structured exception handling to trap

    and handle errors for the given problem statement.

  • 8/14/2019 VB .net tutorial - 11

    10/30

    Exception Handling and Debugging

    NIIT Exception Handling and Debugging/Lesson 11/Slide 10 of 30

    Just a Minute

    What is the difference between the On ErrorGoTo0, OnErrorResumeNext, and OnErrorGoToErrorHandlerstatements and when is each of them used?

  • 8/14/2019 VB .net tutorial - 11

    11/30

    Exception Handling and Debugging

    NIIT Exception Handling and Debugging/Lesson 11/Slide 11 of 30

    Task 2: Write the code to handle errors.

    Task 3: Execute the application to verify the results

    of exception handling.

  • 8/14/2019 VB .net tutorial - 11

    12/30

    Exception Handling and Debugging

    NIIT Exception Handling and Debugging/Lesson 11/Slide 12 of 30

    Problem Statement 11.P.1

    A weekly order details report and a weekly query handlingreport have been created. Based on the users choice, thecorresponding report should be displayed. If the report to beopened gives a loading error, an appropriate message shouldbe displayed and the application should not end abruptly.

  • 8/14/2019 VB .net tutorial - 11

    13/30

    Exception Handling and Debugging

    NIIT Exception Handling and Debugging/Lesson 11/Slide 13 of 30

    Problem Statement 11.D.2

    An application has been created to display the product ID for aproduct that costs more than $3000.

    A Windows Form has been designed with a ListBox controland a Button control.

    A data adapter has been created and the Base_Cost andProdID columns of the table Product have been retrieved. A

    dataset has been generated for the data adapter.

    The code has been written for the Load event of the form todisplay the ProductID for the products with cost more than$3000 in the list box.

    However, the application does not give the expected output.The list box displays one product ID multiple times.

  • 8/14/2019 VB .net tutorial - 11

    14/30

    Exception Handling and Debugging

    NIIT Exception Handling and Debugging/Lesson 11/Slide 14 of 30

    Task List

    Identify the mechanism to trap the error.

    Identify the starting point to search for errors.

    Implement error trapping.

    Run the application and trace the error.

    Rectify the code.

    Verify the output of the code.

  • 8/14/2019 VB .net tutorial - 11

    15/30

    Exception Handling and Debugging

    NIIT Exception Handling and Debugging/Lesson 11/Slide 15 of 30

    Task 1: Identify the mechanism to trap the error.

    Tracing and rectifying an error is termed as debugging.

    In order to assist you in debugging logical and run-time

    errors, Visual Basic .NET provides extensive debuggingtools. You can access these tools by using:

    Debug toolbar

    Debug menu

  • 8/14/2019 VB .net tutorial - 11

    16/30

    Exception Handling and Debugging

    NIIT Exception Handling and Debugging/Lesson 11/Slide 16 of 30

    Task 1: Identify the mechanism to trap the error.(Contd.)

    In an application, by using the debugging tools, you can:

    Start execution

    Break execution

    Stop execution

    Result:

    You will use the debugging tools provided by VisualStudio .NET to trap and rectify the error.

  • 8/14/2019 VB .net tutorial - 11

    17/30

    Exception Handling and Debugging

    NIIT Exception Handling and Debugging/Lesson 11/Slide 17 of 30

    Task 2:Identify the starting point to search for errors.

    Result:

    Since the list box contains a product ID, it indicates that

    there is no error in the database connection. The error trapshould be set starting from the line containing

    OleDbDAProduct.Fill(DsProduct1)command as theerror occurs after the connection is opened.

    Task 3: Implement error trapping.

    Task 4: Run the application and trace the error.

    Two options in the Debug menu to start an application:

    The Start option

    The Start Without Debugging option

  • 8/14/2019 VB .net tutorial - 11

    18/30

    Exception Handling and Debugging

    NIIT Exception Handling and Debugging/Lesson 11/Slide 18 of 30

    Task 4: Run the application and trace the error.(Contd.)

    You may select the Debug menu option and select any ofthe following, to instruct Visual Basic .NET regarding theexecution of the code.

    Step Into

    Step Over

    Step Out

    Task 5: Rectify the code.

    Task 6: Verify the output of the code.

  • 8/14/2019 VB .net tutorial - 11

    19/30

    Exception Handling and Debugging

    NIIT Exception Handling and Debugging/Lesson 11/Slide 19 of 30

    Problem Statement 11.D.3

    An application has been developed to connect to the databaseand retrieve customer details from the database. The codehas been added in the Load event of the form, which willdisplay customer details.

    However, the application generates an error, as it is not ableto connect to the database.

  • 8/14/2019 VB .net tutorial - 11

    20/30

    Exception Handling and Debugging

    NIIT Exception Handling and Debugging/Lesson 11/Slide 20 of 30

    Task List

    Identify the mechanism to debug the code.

    Implement debugging.

    Rectify the code.

    Verify the output of the code.

  • 8/14/2019 VB .net tutorial - 11

    21/30

    Exception Handling and Debugging

    NIIT Exception Handling and Debugging/Lesson 11/Slide 21 of 30

    Task 1: Identify the mechanism to debug the code.Visual Basic provides you with some tools to debug your

    application during the break mode. Two of the debuggingtools are listed below:

    The Localswindow The Immediatewindow

    Result:

    As per the problem statement, the connection with thedatabase server has failed. The connection to the database

    is being made with the help of variables that form part of theconnection string. The connection to the database can fail ifthe values in the variables are not correct.

    To solve this problem, you need to use the Immediatewindow to see the value that the user enters and is stored inthe variable db.

  • 8/14/2019 VB .net tutorial - 11

    22/30

    Exception Handling and Debugging

    NIIT Exception Handling and Debugging/Lesson 11/Slide 22 of 30

    Task 2: Implement debugging.

    Task 3: Rectify the code.

    Task 4: Verify the output of the code.

  • 8/14/2019 VB .net tutorial - 11

    23/30

    Exception Handling and Debugging

    NIIT Exception Handling and Debugging/Lesson 11/Slide 23 of 30

    Problem Statement 11.D.4

    An application has been developed which displays thecorresponding base cost and the product name of the productID selected by the user from a list box. The general level

    declarations have been made and the code has been added.

    However, the application displays the product name and thebase cost of the product ID P014 regardless of the ID selectedby the user.

  • 8/14/2019 VB .net tutorial - 11

    24/30

    Exception Handling and Debugging

    NIIT Exception Handling and Debugging/Lesson 11/Slide 24 of 30

    Task List

    Identify the mechanism to debug the code.

    Implement debugging.

    Rectify the code.

    Verify the output of the code.

  • 8/14/2019 VB .net tutorial - 11

    25/30

    Exception Handling and Debugging

    NIIT Exception Handling and Debugging/Lesson 11/Slide 25 of 30

    Task 1: Identify the mechanism to debug the code.

    The following windows are used for debugging anapplication:

    Watch window Call Stack window

    The Output window

  • 8/14/2019 VB .net tutorial - 11

    26/30

    Exception Handling and Debugging

    NIIT Exception Handling and Debugging/Lesson 11/Slide 26 of 30

    Task 1: Identify the mechanism to debug the code.(Contd.)

    Result:

    Since the variable prodid stores the selected item, its valuehas to be traced. Therefore, you will use the Watch

    window to trace the behavior of prodid.

    Task 2: Implement debugging.

    Task 3: Rectify the code.

    Task 4: Verify the output of the code.

  • 8/14/2019 VB .net tutorial - 11

    27/30

    Exception Handling and Debugging

    NIIT Exception Handling and Debugging/Lesson 11/Slide 27 of 30

    Summary

    In this lesson, you learned that:

    The types of errors that can occur in an application are

    syntax errors, run-time errors, and logical errors.An exception is an abnormal or condition that an application

    encounters during execution.

    Exception handling is the process of providing an alternatepath to be executed when the application is not able toexecute in the desired way.

  • 8/14/2019 VB .net tutorial - 11

    28/30

    Exception Handling and Debugging

    NIIT Exception Handling and Debugging/Lesson 11/Slide 28 of 30

    Summary (Contd.)

    Visual Basic .NET provides two methods to handleexceptions:

    Structured exception handling Unstructured exception handling

    In order to assist you in debugging logical and runtimeerrors, Visual Basic .NET provides extensive debuggingtools. You can use the Visual Basic .NET Debug toolbar toaccess these tools.

  • 8/14/2019 VB .net tutorial - 11

    29/30

    Exception Handling and Debugging

    NIIT Exception Handling and Debugging/Lesson 11/Slide 29 of 30

    Summary (Contd.)

    By using the debugging tools, you can perform the followingactions:

    Start execution Break execution

    While in break mode, the following windows can beused to trace and debug an application:

    Call Stack window

    Watch window

    Locals window Immediate window

  • 8/14/2019 VB .net tutorial - 11

    30/30

    Exception Handling and Debugging

    NIIT Exception Handling and Debugging/Lesson 11/Slide 30 of 30

    Summary (Contd.)Output window

    Step through execution

    Stop execution