Programming Language Concept

Just another Binusian blog site

Session 11 : Exception Handling and Event Handling

December16

Introduction to Exception Handling

  • In a language without exception handling

–When an exception occurs, control goes to the operating system, where a message is displayed and the program is terminated

  • In a language with exception handling

–Programs are allowed to trap some exceptions, thereby providing the possibility of fixing the problem and continuing

Exception Handling in C++

  • Added to C++ in 1990
  • Design is based on that of CLU, Ada, and ML

C++ Exception Handlers

  • Exception Handlers Form:

  try {

— code that is expected to raise an exception

}

catch (formal parameter) {

handler code

}

catch (formal parameter) {

handler code

}

Introduction to Event Handling

  • An event is a notification that something specific has occurred, such as a mouse click on a graphical button
  • The event handler is a segment of code that is executed in response to an event

Event Handling in C#

  • Event handling in C# (and the other .NET languages) is similar to that in Java
  • .NET has two approaches, Windows Forms and Windows Presentation Foundation—we cover only the former (which is the original approach)
  • An application subclasses the Form predefined class (defined in System.Windows.Forms)
  • There is no need to create a frame or panel in which to place the GUI components
  • Label objects are used to place text in the window
  • Radio buttons are objects of the RadioButton class
  • Components are positioned by assigning a new Point object to the Location property of the component  private RadioButton plain = new RadioButton();

    plain.Location = new Point(100, 300);

    plain.Text = ″Plain″;

    controls.Add(plain);

  • All C# event handlers have the same protocol, the return type is void and the two parameters are of types object and EventArgs
  • An event handler can have any name
  • A radio button is tested with the Boolean Checked property of the button

      private void rb_CheckedChanged (object o,

    EventArgs e) {

    if (plain.Checked) …

    }

  • To register an event, a new EventHandler object must be created and added to the predefined delegate for the event
  • When a radio button changes from unchecked to checked, the CheckedChanged event is raised
  • The associated delegate is referenced by the name of the event
  • If the handler was named rb_CheckedChanged, we could register it on the radio button named plain with:

plain.CheckedChanged +=

new EventHandler (rb_CheckedChanged);

Email will not be published

Website example

Your Comment: