Exception handling in Object Oriented Language

Exception handling in Object Oriented Language

Exception Handling is a common concept in object oriented programming languages like C++, Java and C#. Exceptions occur when a program executes abnormally due to conditions outside the program’s control, such as low memory or I/O errors.

The order in which catch handlers appear is significant, because handlers for a given try block are examined in order of their appearance.

After a matching catch handler is found, subsequent handlers are not examined. As a result, an ellipsis (…) catch handler must be the last handler for its try block.

The following is the syntax for exception handling in C++:

try 
// code that may generate exceptions
( throw exceptions)
} catch( type1 id1) {
//handle exceptions of type1
} catch(type2 id2) {
//handle exceptions of type2
}{        

Some sample C++ code snippets to illustrate exception handling:

class Inner {
  public:
    Inner();
    ~Inner();
    void fun();
};
    
class Outer {
  public:
    Outer();
    ~Outer();
    void fun();
};

Inner::Inner()
{
  cout << "Inner constructor\n";
}
  
Inner::~Inner()
{
  cout << "Inner destructor\n";
}
  
void Inner::fun()
{
  cout << "Inner fun\n";
  throw 1; //throw an exception
  cout << "This statement is not executed\n";
}

Outer::Outer()
{
  cout << "Outer constructor\n";
}
  
Outer::~Outer()
{
  cout << "Outer destructor\n";
}
  
void Outer::fun()
{
  Inner in;
  cout << "Outer fun\n";
  in.fun();
}

int main()
{
  Outer out;
  try
  {
    out.fun();
    cout << "\nThis line will also not be executed" << endl;
  }
  catch (int i) {
    cout << "Exception " << i << " occurred" << endl;
  }
  catch (...) { //ellipsis handler, handle any type of exceptions
    cout << ”Exception occurred" << endl;
  }
    
  return 0;
}        

Generated output:

Outer constructor
Inner constructor
Outer fun
Inner fun
Inner destructor
Exception 1 occurred
Outer destructor        

To view or add a comment, sign in

More articles by Dennis Chang

  • Threads and Multi-threaded Programming

    When I heard about the new social media app “Threads” of Meta, the first thing that came to my mind was, did some…

  • Introduction to Cryptography

    Cryptography involves two processes: Encryption and Decryption. Encryption is the process of transforming a plaintext…

    2 Comments
  • Function and Operator Overloading in C++

    Function Overloading: Function Overloading is a programming concept whereby a function can be reused with different…

  • Pointers in C Language

    1. Introduction C is a very powerful programming language that are used by many software developers to develop…

  • Introduction to classes and objects in C++

    C++ had been the most dominant programming language in the 80s, 90s and even early 2000s. Till this day, many academic…

    1 Comment
  • Using DISC for Sales Profiling

    DISC is a behaviour assessment tool based on the DISC theory of psychologist William Marston. Marston’s theory centres…

  • Simple explanation on stack overflow

    Some time ago, I was briefly explaining some abstract data structures (Stacks, queues, linked list, etc) to my mentee…

  • What is Blue Ocean and how to find them?

    1. Introduction Blue ocean strategy is about creating uncontested market space by reconstructing market boundaries and…

  • Which to create: Facebook profile, group or page?

    Facebook is one of, if not the most, popular social media platform in the world. Apart from connecting with your…

    2 Comments

Explore content categories