🔴 Advanced  ·  Lesson 52

Exception Handling

Exception Handling

What is Exception Handling?

Exception Handling
Exception handling manages runtime errors using try, throw and catch. It helps separate error handling from normal program logic.
Level
🔴 STL, Modern C++ and Projects
Example File
exception-handling.cpp
Main Focus
Concept + syntax + practical C++ program

Why should you learn this?

  • It helps you write correct and readable C++ programs.
  • It is used repeatedly in school practicals, projects and competitive programming.
  • It builds the base for advanced topics such as OOP, STL and data structures.

Important Terms

TermMeaning / Use
tryException Handling programming में Try use होता है।
throwException Handling programming में Throw use होता है।
catchException Handling programming में Catch use होता है।
runtime errorException Handling programming में Runtime error use होता है।
exceptionException Handling programming में Exception use होता है।

Syntax / Pattern

C++
try { throw value; } catch(type e) { handle; }

Example Program

main.cpp
#include <iostream>
using namespace std;
int main(){
    int b = 0;
    try{
        if(b == 0) throw "Division by zero";
    } catch(const char* msg){
        cout << msg;
    }
    return 0;
}

Expected Output

Division by zero

Program Explanation

  • throw sends an exception.
  • catch receives and handles it.
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.

Where will you use it?

  • file errors
  • invalid input
  • banking applications

Common Mistakes

  • Using exceptions for normal if-else logic.
  • Catching wrong exception type.

Practice Tasks

  1. Handle invalid age exception.
  2. Explain try-catch flow.

Summary

Exception Handling एक ज़रूरी C++ topic है। परिभाषा सीखें, syntax समझें, example program चलाएं और फिर practice tasks हल करके concept मज़बूत करें।

Exception Handling क्या है?

Exception Handling
Exception handling manages runtime errors using try, throw and catch. It helps separate error handling from normal program logic.
Level
🔴 STL, Modern C++ and Projects
Example File
exception-handling.cpp
मुख्य फोकस
Concept + syntax + practical C++ program

इसे क्यों सीखें?

  • यह आपको सही और पढ़ने-योग्य C++ programs लिखने में मदद करता है।
  • यह school practicals, projects और competitive programming में बार-बार use होता है।
  • यह OOP, STL और data structures जैसे advanced topics की नींव बनाता है।

ज़रूरी Terms

Termअर्थ / उपयोग
tryTry Exception Handling programming में use होता है।
throwThrow Exception Handling programming में use होता है।
catchCatch Exception Handling programming में use होता है।
runtime errorRuntime error Exception Handling programming में use होता है।
exceptionException Exception Handling programming में use होता है।

Syntax / Pattern

C++
try { throw value; } catch(type e) { handle; }

Example Program

main.cpp
#include <iostream>
using namespace std;
int main(){
    int b = 0;
    try{
        if(b == 0) throw "Division by zero";
    } catch(const char* msg){
        cout << msg;
    }
    return 0;
}

Expected Output

Division by zero

Program Explanation

  • throw sends an exception.
  • catch receives and handles it.
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.

Where will you use it?

  • file errors
  • invalid input
  • banking applications

आम गलतियाँ (Common Mistakes)

  • Using exceptions for normal if-else logic.
  • Catching wrong exception type.

अभ्यास (Practice Tasks)

  1. Handle invalid age exception.
  2. Explain try-catch flow.

सारांश

Exception Handling एक ज़रूरी C++ topic है। परिभाषा सीखें, syntax समझें, example program चलाएं और फिर practice tasks हल करके concept मज़बूत करें।

← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

\n

💻 Live Code Editor

Is page ke program yahan ready hain — chalाएं, badlें aur seekhें. Bina kuch install kiye.
Powered by OneCompiler. Editor mein code apne aap aa jata hai — Run dabaakर output dekhें. Agar load na ho to naye tab mein kholें.