You are previewing Illustrated C# 2012.

Illustrated C# 2012

Cover of Illustrated C# 2012 by Daniel Solis Published by Apress
  1. Title
  2. Dedication
  3. Contents at a Glance
  4. Contents
  5. About the Author
  6. About the Technical Reviewer
  7. Acknowledgments
  8. Introduction
  9. CHAPTER 1: C# and the .NET Framework
    1. Before .NET
    2. Enter Microsoft .NET
    3. Compiling to the Common Intermediate Language
    4. Compiling to Native Code and Execution
    5. The Common Language Runtime
    6. The Common Language Infrastructure
    7. Review of the Acronyms
    8. The Evolution of C#
  10. CHAPTER 2: Overview of C# Programming
    1. A Simple C# Program
    2. Identifiers
    3. Keywords
    4. Main: The Starting Point of a Program
    5. Whitespace
    6. Statements
    7. Text Output from a Program
    8. Comments: Annotating the Code
  11. CHAPTER 3: Types, Storage, and Variables
    1. A C# Program Is a Set of Type Declarations
    2. A Type Is a Template
    3. Instantiating a Type
    4. Data Members and Function Members
    5. Predefined Types
    6. User-Defined Types
    7. The Stack and the Heap
    8. Value Types and Reference Types
    9. Variables
    10. Static Typing and the dynamic Keyword
    11. Nullable Types
  12. CHAPTER 4: Classes: The Basics
    1. Overview of Classes
    2. Programs and Classes: A Quick Example
    3. Declaring a Class
    4. Class Members
    5. Creating Variables and Instances of a Class
    6. Allocating Memory for the Data
    7. Instance Members
    8. Access Modifiers
    9. Accessing Members from Inside the Class
    10. Accessing Members from Outside the Class
    11. Putting It All Together
  13. CHAPTER 5: Methods
    1. The Structure of a Method
    2. Code Execution in the Method Body
    3. Local Variables
    4. Local Constants
    5. Flow of Control
    6. Method Invocations
    7. Return Values
    8. The Return Statement and Void Methods
    9. Parameters
    10. Value Parameters
    11. Reference Parameters
    12. Reference Types As Value and Reference Parameters
    13. Output Parameters
    14. Parameter Arrays
    15. Summary of Parameter Types
    16. Method Overloading
    17. Named Parameters
    18. Optional Parameters
    19. Stack Frames
    20. Recursion
  14. CHAPTER 6: More About Classes
    1. Class Members
    2. Order of Member Modifiers
    3. Instance Class Members
    4. Static Fields
    5. Accessing Static Members from Outside the Class
    6. Static Function Members
    7. Other Static Class Member Types
    8. Member Constants
    9. Constants Are Like Statics
    10. Properties
    11. Instance Constructors
    12. Static Constructors
    13. Object Initializers
    14. Destructors
    15. The readonly Modifier
    16. The this Keyword
    17. Indexers
    18. Access Modifiers on Accessors
    19. Partial Classes and Partial Types
    20. Partial Methods
  15. CHAPTER 7: Classes and Inheritance
    1. Class Inheritance
    2. Accessing the Inherited Members
    3. All Classes Are Derived from Class object
    4. Masking Members of a Base Class
    5. Base Access
    6. Using References to a Base Class
    7. Constructor Execution
    8. Inheritance Between Assemblies
    9. Member Access Modifiers
    10. Abstract Members
    11. Abstract Classes
    12. Sealed Classes
    13. Static Classes
    14. Extension Methods
    15. Naming Conventions
  16. CHAPTER 8: Expressions and Operators
    1. Expressions
    2. Literals
    3. Order of Evaluation
    4. Simple Arithmetic Operators
    5. The Remainder Operator
    6. Relational and Equality Comparison Operators
    7. Increment and Decrement Operators
    8. Conditional Logical Operators
    9. Logical Operators
    10. Shift Operators
    11. Assignment Operators
    12. The Conditional Operator
    13. Unary Arithmetic Operators
    14. User-Defined Type Conversions
    15. Operator Overloading
    16. The typeof Operator
    17. Other Operators
  17. CHAPTER 9: Statements
    1. What Are Statements?
    2. Expression Statements
    3. Flow-of-Control Statements
    4. The if Statement
    5. The if...else Statement
    6. The while Loop
    7. The do Loop
    8. The for Loop
    9. The switch Statement
    10. Jump Statements
    11. The break Statement
    12. The continue Statement
    13. Labeled Statements
    14. The goto Statement
    15. The using Statement
    16. Other Statements
  18. CHAPTER 10: Structs
    1. What Are Structs?
    2. Structs Are Value Types
    3. Assigning to a Struct
    4. Constructors and Destructors
    5. Field Initializers Are Not Allowed
    6. Structs Are Sealed
    7. Boxing and Unboxing
    8. Structs As Return Values and Parameters
    9. Additional Information About Structs
  19. CHAPTER 11: Enumerations
    1. Enumerations
    2. Bit Flags
    3. More About Enums
  20. CHAPTER 12: Arrays
    1. Arrays
    2. Types of Arrays
    3. An Array As an Object
    4. One-Dimensional and Rectangular Arrays
    5. Instantiating a One-Dimensional or Rectangular Array
    6. Accessing Array Elements
    7. Initializing an Array
    8. Jagged Arrays
    9. Comparing Rectangular and Jagged Arrays
    10. The foreach Statement
    11. Array Covariance
    12. Useful Inherited Array Members
    13. Comparing Array Types
  21. CHAPTER 13: Delegates
    1. What Is a Delegate?
    2. An Overview of Delegates
    3. Declaring the Delegate Type
    4. Creating the Delegate Object
    5. Assigning Delegates
    6. Combining Delegates
    7. Adding Methods to Delegates
    8. Removing Methods from a Delegate
    9. Invoking a Delegate
    10. Delegate Example
    11. Invoking Delegates with Return Values
    12. Invoking Delegates with Reference Parameters
    13. Anonymous Methods
    14. Lambda Expressions
  22. CHAPTER 14: Events
    1. Publishers and Subscribers
    2. Overview of Source Code Components
    3. Declaring an Event
    4. Subscribing to an Event
    5. Raising an Event
    6. Standard Event Usage
    7. Event Accessors
  23. CHAPTER 15: Interfaces
    1. What Is an Interface?
    2. Declaring an Interface
    3. Implementing an Interface
    4. An Interface Is a Reference Type
    5. Using the as Operator with Interfaces
    6. Implementing Multiple Interfaces
    7. Implementing Interfaces with Duplicate Members
    8. References to Multiple Interfaces
    9. An Inherited Member As an Implementation
    10. Explicit Interface Member Implementations
    11. Interfaces Can Inherit Interfaces
    12. Example of Different Classes Implementing an Interface
  24. CHAPTER 16: Conversions
    1. What Are Conversions?
    2. Implicit Conversions
    3. Explicit Conversions and Casting
    4. Types of Conversions
    5. Numeric Conversions
    6. Reference Conversions
    7. Boxing Conversions
    8. Unboxing Conversions
    9. User-Defined Conversions
    10. The is Operator
    11. The as Operator
  25. CHAPTER 17: Generics
    1. What Are Generics?
    2. Generics in C#
    3. Generic Classes
    4. Declaring a Generic Class
    5. Creating a Constructed Type
    6. Creating Variables and Instances
    7. Constraints on Type Parameters
    8. Generic Methods
    9. Extension Methods with Generic Classes
    10. Generic Structs
    11. Generic Delegates
    12. Generic Interfaces
    13. Covariance
    14. Contravariance
  26. CHAPTER 18: Enumerators and Iterators
    1. Enumerators and Enumerable Types
    2. The IEnumerator Interface
    3. The Generic Enumeration Interfaces
    4. Iterators
    5. Common Iterator Patterns
    6. Producing Multiple Enumerables
    7. Iterators As Properties
    8. Behind the Scenes with Iterators
  27. CHAPTER 19: Introduction to LINQ
    1. What Is LINQ?
    2. LINQ Providers
    3. Method Syntax and Query Syntax
    4. Query Variables
    5. The Structure of Query Expressions
    6. The Standard Query Operators
    7. LINQ to XML
  28. CHAPTER 20: Introduction to Asynchronous Programming
    1. What Is Asynchrony?
    2. The Structure of the async/await Feature
    3. What Is An async Method?
    4. Async Operations in GUI Programs
    5. Using an async Lambda Expression
    6. A Full GUI Example
    7. The BackgroundWorker Class
    8. Parallel Loops
    9. Other Asynchronous Programming Patterns
    10. BeginInvoke and EndInvoke
    11. Timers
  29. CHAPTER 21: Namespaces and Assemblies
    1. Referencing Other Assemblies
    2. Namespaces
    3. The using Directives
    4. The Structure of an Assembly
    5. The Identity of an Assembly
    6. Strongly Named Assemblies
    7. Private Deployment of an Assembly
    8. Shared Assemblies and the GAC
    9. Configuration Files
    10. Delayed Signing
  30. CHAPTER 22: Exceptions
    1. What Are Exceptions?
    2. The try Statement
    3. The Exception Classes
    4. The catch Clause
    5. Examples Using Specific catch Clauses
    6. The catch Clauses Section
    7. The finally Block
    8. Finding a Handler for an Exception
    9. Searching Further
    10. Throwing Exceptions
    11. Throwing Without an Exception Object
  31. CHAPTER 23: Preprocessor Directives
    1. What Are Preprocessor Directives?
    2. General Rules
    3. The #define and #undef Directives
    4. Conditional Compilation
    5. The Conditional Compilation Constructs
    6. Diagnostic Directives
    7. Line Number Directives
    8. Region Directives
    9. The #pragma warning Directive
  32. CHAPTER 24: Reflection and Attributes
    1. Metadata and Reflection
    2. The Type Class
    3. Getting a Type Object
    4. What Is an Attribute?
    5. Applying an Attribute
    6. Predefined, Reserved Attributes
    7. More About Applying Attributes
    8. Custom Attributes
    9. Accessing an Attribute
  33. CHAPTER 25: Other Topics
    1. Overview
    2. Strings
    3. The StringBuilder Class
    4. Parsing Strings to Data Values
    5. More About the Nullable Types
    6. Method Main
    7. Documentation Comments
    8. Nested Types
    9. Destructors and the Dispose Pattern
    10. Interoperating with COM
  34. Index
O'Reilly logo

C H A P T E R  22

Exceptions

What Are Exceptions?

An exception is a runtime error in a program that violates a system or application constraint, or a condition that is not expected to occur during normal operation. Examples are when a program tries to divide a number by zero or tries to write to a read-only file. When these occur, the system catches the error and raises an exception.

If the program has not provided code to handle the exception, the system will halt the program. ...

The best content for your career. Discover unlimited learning on demand for around $1/day.