Book contents
- Frontmatter
- Contents
- Preface
- Chapter 1 An Overview of the Visual Basic.NET Language
- Chapter 2 An Overview of Object-Oriented Programming
- Chapter 3 Structures
- Chapter 4 Classes
- Chapter 5 Access Modifiers
- Chapter 6 Abstract Classes and Interfaces
- Chapter 7 Implementing the IEnumerable and IComparable Interfaces
- Chapter 8 Designing and Implementing Exception Classes
- Chapter 9 Design Patterns and Refactoring
- Chapter 10 Object Internals: Reflection and Attributes
- Chapter 11 Object Persistence: Serialization
- Chapter 12 Building a Windows Application
- Chapter 13 Database Programming Using ADO.NET
- References
- Index
Chapter 8 - Designing and Implementing Exception Classes
Published online by Cambridge University Press: 06 July 2010
- Frontmatter
- Contents
- Preface
- Chapter 1 An Overview of the Visual Basic.NET Language
- Chapter 2 An Overview of Object-Oriented Programming
- Chapter 3 Structures
- Chapter 4 Classes
- Chapter 5 Access Modifiers
- Chapter 6 Abstract Classes and Interfaces
- Chapter 7 Implementing the IEnumerable and IComparable Interfaces
- Chapter 8 Designing and Implementing Exception Classes
- Chapter 9 Design Patterns and Refactoring
- Chapter 10 Object Internals: Reflection and Attributes
- Chapter 11 Object Persistence: Serialization
- Chapter 12 Building a Windows Application
- Chapter 13 Database Programming Using ADO.NET
- References
- Index
Summary
Programs written in VB.NET utilize the Throw-Catch model of exception handling when dealing with errors. The classes found in the.NET Framework use this model for handling errors, but the classes you develop must generate their own exceptions for handling errors. In this chapter, we discuss how exception classes are created and how to use them. We start the chapter with a review of the Throw-Catch model, which includes the Try-Catch-Finally statement.
EXCEPTION HANDLING IN VB.NET
The term VB.NET uses for errors that occur in executing code is exception. Writing code that deals with errors in a program is called exception handling. Exception handling in VB.NET consists of writing code that watches for exceptions when they're thrown and writing code that causes an exception to be thrown when an error condition arises. A VB.NET programmer is not responsible for always writing exception-generating code since the.NET Framework classes throw their own exceptions.
For example, trying to open a file that doesn't exist throws an exception because there was no file to open. This object is called FileNotFoundException. In the next section we examine how to write code to catch built-in exception objects.
Writing Exception-Handling Code
As we've discussed, trying to open a nonexistent file throws an exception. For our program to recognize the exception, we have to use a special construct—the Try-Catch-Finally statement.
- Type
- Chapter
- Information
- Object-Oriented Programming with Visual Basic.NET , pp. 192 - 201Publisher: Cambridge University PressPrint publication year: 2004