CS608 Lecture Notes Visual Basic.NET Programming Object-Oriented Programming Classes & Objects – Advanced Topics Part III (Error Handling) (Part III of III) (Lecture Notes 2C) Prof. Abel Angel Rodriguez2 CHAPTER 7 CLASSES & OBJECTS -ADVANCED CONCEPTS (CONT)......................................... 3 7.1 Error Handling in VB.NET............................................................................................................................................................... 3 7.1.1 General Discussion on Errors........................................................................................................................................................ 3 Syntax Errors ..................................................................................................................................................................................... 3 Logical Errors .................................................................................................................................................................................... 3 Run-Time Errors ................................................................................................................................................................................ 3 7.2 Exception in VB.NET ........................................................................................................................................................................ 6 7.2.1 Exceptions Basics in VB.NET ...................................................................................................................................................... 6 The Exit Try Statement ...................................................................................................................................................................... 8 7.2.2 The VB.NET Exception Class .................................................................................................................................................... 10 Introduction to the VB.NET Exception Class .................................................................................................................................. 10 Trapping all Errors using the VB.NET Exception Class.................................................................................................................. 11 7.2.3 Trapping for Specific Error Using other Exception Classes Provided by VB.NET .................................................................... 15 Trapping for Specific Error using the Exception Classes ................................................................................................................ 16 7.2.4 Trapping for Many Error using Multiple Catch Blocks .............................................................................................................. 22 7.2.5 The Throw Statement .................................................................................................................................................................. 24 Error Handling Summarized ............................................................................................................................................................ 24 Raising our own errors ..................................................................................................................................................................... 24 Using the Throw Statement.............................................................................................................................................................. 25 7.3 Shared Methods ............................................................................................................................................................................... 30 7.3.1 Overview & Discussion .............................................................................................................................................................. 30 7.3.2 Shared Methods .......................................................................................................................................................................... 31 7.4 Shared Variables .............................................................................................................................................................................. 32 7.4.1 Overview ..................................................................................................................................................................................... 323 Chapter 7 Classes & Objects -Advanced Concepts (Cont) 7.1 Error Handling in VB.NET 7.1.1 General Discussion on Errors In our review of VB.NET we discussed the various types of errors that can occur in a program. There are three varieties of programming errors: 1. Syntax 2. Logical 3. Run-Time Syntax Errors Syntax Errors – Errors generated due to the code not following the rules of the language These errors usually involve improper punctuation, spelling or format These problems are easier to solve since the compiler identifies the erroneous statement by displaying a BLUE Wiggly Line under the incorrect VB.NET Statement. To resolve this problem, you just have to review the rules of the language for that statement and make sure you are writing the statement properly Logical Errors Logical Error – The program is not doing what is supposed to do. . The algorithm fails to solve the problem the program was written to resolve These problems are very difficult to solve since you need to re-think and go back to the planning phase and review the Algorithm. Run-Time Errors Run Time Errors – These are programs that occur when the program is executing. The program passed the syntax or compiler test but fails during execution. . These errors can be caused by the user entering a wrong data type via keyboard or performing some illegal operation, program logic not doing what is supposed to do, or system errors. These problems are usually caused by improper arithmetic execution, attempting to access recourses that are not available etc These problems are difficult to solve since they only show up when the program runs. When a run-time error is generated, the built-in default Error handling mechanism of VB.NET will trap the error and terminate the
View Full Document