Introduction

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

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

Comparison of Exception Handling in C++ and Java

Both languages use trycatch and throw keywords for exception handling, and meaning of trycatch and free blocks is also same in both languages. Following are the differences between Java and C++ exception handling.

1) In C++, all types (including primitive and pointer) can be thrown as exception. But in Java only throwable objects (Throwable objects are instances of any subclass of the Throwable class) can be thrown as exception. For example, following type of code works in C++, but similar code doesn’t work in Java.

#include <iostream>
using namespace std;
int main()
{
   int x = -1;
   // some other stuff  
   try {
      // some other stuff
      if( x < 0 )
      {
         throw x;
      }  
   }
   catch (int x ) {
      cout << "Exception occurred: thrown value is " << x << endl;
   }
   getchar();
   return 0;
}

Output:
Exception occurred: thrown value is -1

2) In C++, there is a special catch called “catch all” that can catch all kind of exceptions.

#include <iostream>
using namespace std;
int main()
{
   int x = -1;
   char *ptr;
   
   ptr = new char[256];
   
   // some other stuff  
   try {
      // some other stuff
      if( x < 0 )
      {
         throw x;
      }     
      if(ptr == NULL)
      {
         throw " ptr is NULL ";
      }  
   }
   catch (...) // catch all
   {
      cout << "Exception occurred: exiting "<< endl;
      exit(0);
   }
   
   getchar();
   return 0;
}

Output:
Exception occurred: exiting

In Java, for all practical purposes, we can catch Exception object to catch all kind of exceptions. Because, normally we do not catch Throwable(s) other than Exception(s) (which are Errors)

catch(Exception e){
  …….
}

3) In Java, there is a block called finally that is always executed after the try-catch block. This block can be used to do cleanup work. There is no such block in C++.

// creating an exception type
class Test extends Exception { }
class Main {
   public static void main(String args[]) {
      try {
         throw new Test();
      }
      catch(Test t) {
         System.out.println("Got the Test Exception");
      }
      finally {
         System.out.println("Inside finally block ");
      }
  }
}

Output:
Got the error
Inside finally block

4) In C++, all exceptions are unchecked. In Java, there are two types of exceptions – checked and unchecked. See this for more details on checked vs Unchecked exceptions.

5) In Java, a new keyword throws is used to list exceptions that can be thrown by a function. In C++, there is no throws keyword, the same keyword throw is used for this purpose also.

Event Handling

Java:

  • One class of events is ItemEvent, which is associated with the event of clicking a checkbox, a radio button, or a list item
  • The ItemListener interface prescribes a method, itemStateChanged, which is a handler for ItemEvent events
  • The listener is created with addItemListener

C#:

  • An application subclasses the Form predefined class (defined in System.Windows.Forms)
  • Label objects are used to place text in the window
  • Radio buttons are objects of the RadioButton class
  • 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);