Download presentation
Presentation is loading. Please wait.
Published byMary Johnson Modified over 9 years ago
1
Understanding Events and Exceptions Lesson 3
2
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)
3
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.
4
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:
5
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.
6
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.
7
Publishing an Event
8
Subscribing to Events The signature of the event handler method matches the requirements of the event’s delegate.
9
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.
10
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.
11
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.
12
try-catch-finally 2
13
try-finally The finally block can immediately follow the try block if no exception handling other than cleanup is needed.
14
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.
15
Sample Configuration Data
16
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.
17
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.
18
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.