Vb .net Tutorial - 11

  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Vb .net Tutorial - 11 as PDF for free.

More details

  • Words: 1,777
  • Pages: 30
Exception Handling and Debugging Objectives In this lesson, you will learn to: ☛Identify the mechanism to handle run time errors ☛Differentiate between structured and unstructured exception handling ☛Handle exceptions ☛Debug an application

©NIIT

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

Exception Handling and Debugging Problem Statement 11.D.1 While executing the application for adding order details, if the server returns an error or there is insufficient memory to execute the application, an appropriate message should be displayed and the application should not terminate abruptly.

©NIIT

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

Exception Handling and Debugging Task List ☛Identify the mechanism to handle errors. ☛Write the code to handle errors. ☛Execute the application to verify the results of exception handling.

©NIIT

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

Exception Handling and Debugging Task 1: Identify the mechanism to handle errors. ☛An exception is termed as an abnormal condition encountered by an application during execution. ☛Exception handling ✓ Is the process of providing an alternate path to be executed when the application is unable to execute in the desired way.

©NIIT

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

Exception Handling and Debugging Task 1: Identify the mechanism to handle errors. (Contd.) ☛Structured exception handling ✓ All the exceptions are derived from the System.Exception class. System.Exception

System.ApplicationException

System.Windows.Forms.AxHost.InvalidActiveXStateException

System.Runtime.Remoting.MetadataServices.SUDSParserException

System.IO.IsolatedStorage.IsolatedStorageException

System.Runtime.Remoting.MetadataServices.SUDSGeneratorException

System.SystemException

©NIIT

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

Exception Handling and Debugging Task 1: Identify the mechanism to handle errors. (Contd.) ☛Structured exception handling (Contd.) ✓ Divides an application into blocks of code that have the probability of raising an error. ✓ Uses the Try…Catch…Finally statement. ✓ Errors are filtered in the Catch block.

©NIIT

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

Exception Handling and Debugging Just a Minute… In the Try…Catch…Finally statement, when is the code given in each of the blocks executed?

©NIIT

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

Exception Handling and Debugging Task 1: Identify the mechanism to handle errors. (Contd.) ☛User-defined exceptions ✓ Can be created by deriving a class from the ApplicationException 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 pass control to the next executable statement when an error occurs. ✓ Uses the On Error GoTo 0 to disable an error handler. ©NIIT

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

Exception Handling and Debugging Task 1: Identify the mechanism to handle errors. (Contd.) Result: ☛Since unstructured exception handling code is difficult to maintain, you will use structured exception handling to trap and handle errors for the given problem statement.

©NIIT

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

Exception Handling and Debugging Just a Minute… What is the difference between the On Error GoTo 0, On Error Resume Next, and On Error GoTo ErrorHandler statements and when is each of them used?

©NIIT

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

Exception Handling and Debugging Task 2: Write the code to handle errors. Task 3: Execute the application to verify the results of exception handling.

©NIIT

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

Exception Handling and Debugging Problem Statement 11.P.1 A weekly order details report and a weekly query handling report have been created. Based on the user’s choice, the corresponding report should be displayed. If the report to be opened gives a loading error, an appropriate message should be displayed and the application should not end abruptly.

©NIIT

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

Exception Handling and Debugging Problem Statement 11.D.2 An application has been created to display the product ID for a product that costs more than $3000. A Windows Form has been designed with a ListBox control and a Button control. A data adapter has been created and the Base_Cost and ProdID 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 to display 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. ©NIIT

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

Exception Handling and Debugging 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.

©NIIT

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

Exception Handling and Debugging 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 debugging tools. You can access these tools by using: ✓ Debug toolbar ✓ Debug menu

©NIIT

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

Exception Handling and Debugging 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 Visual Studio .NET to trap and rectify the error.

©NIIT

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

Exception Handling and Debugging 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 trap should be set starting from the line containing OleDbDAProduct.Fill(DsProduct1)command as the error 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 ©NIIT

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

Exception Handling and Debugging Task 4: Run the application and trace the error. (Contd.) ☛You may select the Debug menu option and select any of the following, to instruct Visual Basic .NET regarding the execution of the code. ✓ Step Into ✓ Step Over ✓ Step Out

Task 5: Rectify the code. Task 6: Verify the output of the code.

©NIIT

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

Exception Handling and Debugging Problem Statement 11.D.3 An application has been developed to connect to the database and retrieve customer details from the database. The code has been added in the Load event of the form, which will display customer details. However, the application generates an error, as it is not able to connect to the database.

©NIIT

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

Exception Handling and Debugging Task List ☛Identify the mechanism to debug the code. ☛Implement debugging. ☛Rectify the code. ☛Verify the output of the code.

©NIIT

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

Exception Handling and Debugging 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 debugging tools are listed below: ✓ The Locals window ✓ The Immediate window Result: ☛As per the problem statement, the connection with the database server has failed. The connection to the database is being made with the help of variables that form part of the connection string. The connection to the database can fail if the values in the variables are not correct. To solve this problem, you need to use the Immediate window to see the value that the user enters and is stored in the variable db. ©NIIT

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

Exception Handling and Debugging Task 2: Implement debugging. Task 3: Rectify the code. Task 4: Verify the output of the code.

©NIIT

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

Exception Handling and Debugging Problem Statement 11.D.4 An application has been developed which displays the corresponding base cost and the product name of the product ID 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 the base cost of the product ID P014 regardless of the ID selected by the user.

©NIIT

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

Exception Handling and Debugging Task List ☛Identify the mechanism to debug the code. ☛Implement debugging. ☛Rectify the code. ☛Verify the output of the code.

©NIIT

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

Exception Handling and Debugging Task 1: Identify the mechanism to debug the code. ☛The following windows are used for debugging an application: ✓ Watch window ✓ Call Stack window ✓ The Output window

©NIIT

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

Exception Handling and Debugging Task 1: Identify the mechanism to debug the code. (Contd.) Result: ☛Since the variable prodid stores the selected item, its value has 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.

©NIIT

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

Exception Handling and Debugging 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 alternate path to be executed when the application is not able to execute in the desired way.

©NIIT

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

Exception Handling and Debugging Summary (Contd.) ☛Visual Basic .NET provides two methods to handle exceptions: ✓ Structured exception handling ✓ Unstructured exception handling ☛In order to assist you in debugging logical and runtime errors, Visual Basic .NET provides extensive debugging tools. You can use the Visual Basic .NET Debug toolbar to access these tools.

©NIIT

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

Exception Handling and Debugging Summary (Contd.) ☛By using the debugging tools, you can perform the following actions: ✓ Start execution ✓ Break execution ➤ While

in break mode, the following windows can be used to trace and debug an application: ➤ Call

Stack window

➤ Watch

window

➤ Locals

window

➤ Immediate ©NIIT

window

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

Exception Handling and Debugging Summary (Contd.) ➤ Output

window

✓ Step through execution ✓ Stop execution

©NIIT

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

Related Documents