Understanding Events and Exceptions Lesson 3
Objective Domain Matrix Skills/ConceptsMTA Exam Objectives Understand events and event handling Understand Events and Event Handling in the.NET Framework (1.2) Understand structured exception handling Understand Structured Exception Handling in the.NET Framework (1.3) Understand basic application settings Understand Basic Application Settings (1.1)
Delegates – 1 A delegate is special type that can hold a reference to a method. MyDelegate can hold reference to any method that returns nothing and accepts two string parameters.
Delegates – 2 A delegate variable can refer to a method with the same signature as the delegate type. You can indirectly call the Method1 by using the handler delegate variable:
Events Events are a way for a object to notify other objects when something of interest happens. The object that sends the notification is called as a publisher of the event. The object that receives the notification is called the subscriber of the event.
Events - 2 Events are defined using delegates. EventHandler is a predefined handler, which is commonly used for defining events. EventArgs class and its derived types pass event related data to event subscribers.
Publishing an Event
Subscribing to Events The signature of the event handler method matches the requirements of the event’s delegate.
Structured Exception Handling An exception is an error condition that occurs during the execution of a program. When error occurs, the runtime creates an exception object to represent the error and “throws” it. Unless you “catch” the exception by writing proper exception handling code, the program execution will terminate.
try-catch Place the code that throws exceptions inside a try block Place the code that handles the exceptions inside one or more catch blocks immediately below the try block.
try-catch-finally 1 A try block must have at least a catch block or a finally block associated with it. The finally block is always executed regardless of whether an exception is thrown. The finally block is often used to write clean-up code. If present, the finally block, follows the last catch block.
try-catch-finally 2
try-finally The finally block can immediately follow the try block if no exception handling other than cleanup is needed.
Basic Application Settings Application settings store custom application- specific data. Application settings allows you to change certain program settings at runtime without the need to modify the program’s source code. The settings data is stored as XML in a disk file. For Web applications, you store the application settings in the web.config file. For Windows-based applications, the application settings are specified in an app.config file.
Sample Configuration Data
Accessing Application Configuration Use the ConfigurationManager class to access the settings from the application configuration file. The AppSettings property gets the data from the appSettings section of the configuration file. The ConnectionStrings property gets the data from the connectionStrings section of the configuration file.
Accessing Web Application Configuration Use the WebConfigurationManager class to access the settings from the application configuration file. The AppSettings property gets the data from the appSettings section of the configuration file. The ConnectionStrings property gets the data from the connectionStrings section of the configuration file.
Recap Events and Event Handling Delegates Publishing events Subscribing to events Structured Exception Handling Exception handling The try-catch block The try-catch-finally block The try-finally block Basic Application Settings Application configuration Web application configuration