MDU BCA 2nd Sem C++: Exception Handling & Virtual Functions Exam-Ready Code Blocks Notes

Quick Answer: What Are Exception Handling and Virtual Functions in C++?

Exception Handling in C++ is a mechanism used to detect and manage runtime errors so that a program does not terminate unexpectedly. It uses the try, throw, and catch keywords to identify and handle exceptions.

Virtual Functions are member functions declared with the virtual keyword in a base class. They enable runtime polymorphism, allowing the program to decide which function to execute during runtime based on the object type.

For MDU BCA 2nd Semester mdu bca c++ exception handling notes students, both topics are important because they are frequently asked in university examinations, practical exams, and viva sessions.

Introduction

C++ is one of the most important programming subjects in the MDU BCA curriculum. Among all Object-Oriented Programming concepts, Exception Handling and Virtual Functions are considered highly important for both theoretical and practical examinations.

Students often face difficulties understanding how runtime errors are managed and how runtime polymorphism actually works. This guide from Easy Study Notes simplifies these concepts using exam-oriented explanations, practical code examples, important questions, and revision notes.

By the end of this article, you will be able to:

  • Understand Exception Handling in C++
  • Write programs using try, throw, and catch
  • Explain Virtual Functions confidently
  • Understand Runtime Polymorphism
  • Prepare for MDU BCA examinations effectively

Exception Handling in C++ – Complete Notes for MDU BCA

What is Exception Handling?

Exception Handling is a technique used to handle errors that occur during the execution of a program. Instead of abruptly terminating the program, exception handling allows developers to detect and manage errors gracefully.

Definition

Exception Handling is a mechanism that enables a program to detect, throw, and handle runtime errors using try, throw, and catch blocks.

Real-Life Example

Imagine you are withdrawing money from an ATM.

If your balance is sufficient, the transaction succeeds.

If your balance is insufficient, the ATM displays an error message instead of crashing.

Similarly, exception handling helps programs respond to errors without stopping unexpectedly.

Why is Exception Handling Needed?

Without exception handling:

  • Programs may crash suddenly.
  • Users may lose data.
  • Debugging becomes difficult.
  • Software reliability decreases.
  • With exception handling:
  • Errors are handled properly.
  • Programs remain stable.
  • User experience improves.
  • Applications become more reliable.

Common Runtime Errors in C++

Some common runtime errors include:

  • Error Type
  • Description
  • Divide by Zero
  • Attempting to divide a number by zero
  • Invalid Input
  • Entering unexpected values
  • Memory Allocation Failure
  • Insufficient memory availability
  • File Access Error
  • File not found or inaccessible

Components of Exception Handling

C++ uses three important keywords:

1. try Block

The try block contains code that may generate an exception.

C++

try

{

   // risky code

}

2. throw Statement

The throw keyword is used to generate an exception.

throw value;

Example:

throw 10;

3. catch Block

The catch block handles the exception thrown by the program.

catch(int x)

{

   cout<<“Exception Caught”;

}

Syntax of Exception Handling

try

{

   // code that may cause error

}

catch(data_type variable)

{

   // exception handling code

}

Exam Point

Remember the sequence:

try → throw → catch

This question is frequently asked in 2-mark and 5-mark examinations.

Exam-Ready Exception Handling Programs

Program 1: Divide by Zero Exception

#include<iostream>

using namespace std;

int main()

{

   int a=10,b=0;

   try

   {

      if(b==0)

         throw b;

      cout<<a/b;

   }

   catch(int)

   {

      cout<<“Division by zero is not allowed.”;

   }

   return 0;

}

Output

Division by zero is not allowed.

Explanation

The program checks whether the denominator is zero. If it is zero, an exception is thrown and handled by the catch block.

Program 2: Multiple Catch Blocks

#include<iostream>

using namespace std;

int main()

{

   try

   {

      throw ‘A’;

   }

   catch(int)

   {

      cout<<“Integer Exception”;

   }

   catch(char)

   {

      cout<<“Character Exception”;

   }

}

Output

Character Exception

Explanation

Multiple catch blocks allow different exception types to be handled separately.

Program 3: Nested Try-Catch

#include<iostream>

using namespace std;

int main()

{

   try

   {

      try

      {

         throw 100;

      }

      catch(int x)

      {

         cout<<“Inner Catch: “<<x<<endl;

         throw;

      }

   }

   catch(int x)

   {

      cout<<“Outer Catch: “<<x;

   }

   return 0;

}

Output

Inner Catch: 100

Outer Catch: 100

Program 4: User-Defined Exception

#include<iostream>

using namespace std;

int main()

{

   int age;

   cout<<“Enter Age: “;

   cin>>age;

   try

   {

      if(age<18)

         throw age;

      cout<<“Eligible”;

   }

   catch(int age)

   {

      cout<<“Not Eligible. Age = “<<age;

   }

}

Output

Not Eligible. Age = 15

Advantages of Exception Handling

Benefits

  • Prevents abnormal termination
  • Improves software quality
  • Simplifies debugging
  • Increases reliability
  • Provides structured error handling

Limitations

  • Slight performance overhead
  • Improper usage can increase complexity

Virtual Functions in C++ – Complete Notes

What is a Virtual Function?

A Virtual Function is a member function declared in the base class using the virtual keyword.

It allows a derived class function to override the base class function and enables runtime decision-making.

Definition

A virtual function is a member function that supports runtime polymorphism by allowing derived class implementations to be called through a base class pointer.

Why Do We Use Virtual Functions?

Virtual Functions help in:

  • Dynamic Binding
  • Runtime Polymorphism
  • Flexible Program Design
  • Better Code Reusability

Syntax of Virtual Function

class Base

{

public:

   virtual void display()

   {

      cout<<“Base Class”;

   }

};

The keyword virtual informs the compiler that the function may be overridden by derived classes.

Runtime Polymorphism in C++ BCA Notes

What is Runtime Polymorphism?

Runtime Polymorphism is the ability of a program to decide which function should execute during runtime.

It is achieved through:

  • Inheritance
  • Function Overriding
  • Virtual Functions
  • Simple Definition
  • One interface, multiple implementations selected at runtime.

Real-Life Example

Consider a payment application.

The same “Pay” button may:

Process a Credit Card payment

Process a UPI payment

Process a Net Banking payment

The exact implementation is decided at runtime.

This is the concept behind runtime polymorphism.

             Compile-Time vs Runtime Polymorphism

FeatureCompile-TimePolymorphismRuntime Polymorphism
BindingEarly BindingLate Binding
DecisionDuring CompilationDuring Execution
ExampleFunction OverloadingVirtual Function
FlexibilityLowerHigher

C++ Virtual Functions Examples for MDU Exam

Program 1: Basic Virtual Function Example

#include<iostream>

using namespace std;

class Base

{

public:

   virtual void display()

   {

      cout<<“Base Class”;

   }

};

class Derived : public Base

{

public:

   void display()

   {

      cout<<“Derived Class”;

   }

};

int main()

{

   Base *ptr;

   Derived obj;

   ptr=&obj;

   ptr->display();

   return 0;

}

Output

Derived Class

Explanation

The base class pointer points to the derived class object. Due to the virtual function, the derived class version executes.

Program 2: Runtime Polymorphism Example

#include<iostream>

using namespace std;

class Animal

{

public:

   virtual void sound()

   {

      cout<<“Animal Sound”;

   }

};

class Dog : public Animal

{

public:

   void sound()

   {

      cout<<“Dog Barks”;

   }

};

int main()

{

   Animal *a;

   Dog d;

   a=&d;

   a->sound();

}

Output

Dog Barks

Program 3: Student Management Example

#include<iostream>

using namespace std;

class Student

{

public:

   virtual void details()

   {

      cout<<“Student Information”;

   }

};

class BCA : public Student

{

public:

   void details()

   {

      cout<<“MDU BCA Student”;

   }

};

int main()

{

   Student *s;

   BCA b;

   s=&b;

   s->details();

}

Output

MDU BCA Student

Program 4: Employee Salary Example

#include<iostream>

using namespace std;

class Employee

{

public:

   virtual void salary()

   {

      cout<<“Base Salary”;

   }

};

class Manager : public Employee

{

public:

   void salary()

   {

      cout<<“Manager Salary”;

   }

};

int main()

{

   Employee *e;

   Manager m;

   e=&m;

   e->salary();

}

Output

Manager Salary

Difference Between Function Overriding and Virtual Functions

FeatureFunction OverridingVirtual Function
PurposeRedefines functionEnables runtime binding
KeywordNot mandatoryMandatory
Runtime SupportLimitedFull
PolymorphismPartialComplete

Difference Between Exception Handling and Virtual Functions

Exception HandlingVirtual Functions
Handles Runtime ErrorsEnables Runtime Polymorphism
Uses try-catchUses virtual keyword
Improves ReliabilityImproves Flexibility
Focuses on ErrorsFocuses on Behavior

MDU BCA 2nd Sem C++ Important Questions

Short Answer Questions

Define Exception Handling.

What is a try block?

What is a throw statement?

What is a catch block?

Define Virtual Function.

What is Runtime Polymorphism?

What is Dynamic Binding?

Long Answer Questions

Explain Exception Handling with examples.

Explain try, throw, and catch.

Discuss Virtual Functions with syntax.

Explain Runtime Polymorphism with programs.

Differentiate between compile-time and runtime polymorphism.

Frequently Asked University Questions

Write a C++ program for Exception Handling.

Explain Virtual Functions with a suitable example.

What is Runtime Polymorphism? Explain with code.

Differentiate between Function Overloading and Virtual Functions.

Discuss advantages of Exception Handling.

Viva Questions and Answers

What is an exception?

An exception is a runtime error that interrupts the normal flow of a program.

Why is catch used?

The catch block handles exceptions thrown by the program.

What is a Virtual Function?

A virtual function is a function that supports runtime polymorphism.

What is Dynamic Binding?

Dynamic Binding is the process of selecting the function to execute during runtime.

Why is Runtime Polymorphism important?

It provides flexibility and extensibility in object-oriented programming.

One-Day Exam Revision Notes

Exception Handling

  • try block contains risky code.
  • throw generates exceptions.
  • catch handles exceptions.
  • Multiple catch blocks are allowed.
  • Prevents abnormal program termination.

Virtual Functions

  • Declared using virtual keyword.
  • Enable runtime polymorphism.
  • Support late binding.
  • Commonly used with base class pointers.

Frequently asked in MDU examinations.

Frequently Asked Questions (FAQs)

What is Exception Handling in C++?

Exception Handling is a mechanism that manages runtime errors using try, throw, and catch blocks.

What are Virtual Functions in C++?

Virtual Functions are functions declared with the virtual keyword that support runtime polymorphism.

What is Runtime Polymorphism?

Runtime Polymorphism allows the program to choose the appropriate function during execution rather than during compilation.

Why are Virtual Functions important in MDU BCA exams?

Virtual Functions are a core Object-Oriented Programming concept and are frequently included in theory, practical, and viva examinations.

Can I prepare this topic in one day?

Yes. By studying the definitions, syntax, important programs, and revision notes provided in this guide, most students can prepare this topic effectively within one day.

Conclusion

Understanding Exception Handling and Virtual Functions is essential for scoring well in the MDU BCA 2nd Semester C++ examination. Exception Handling helps create reliable programs by managing runtime errors, while Virtual Functions make runtime polymorphism possible through dynamic binding.
At Easy Study Notes, we recommend practicing each code example, revising the important questions, and understanding the concepts behind the programs rather than memorizing them. This approach will help you perform better in university exams, practicals, and viva sessions while building a strong foundation in Object-Oriented Programming using C++.