try, catch, throw

Exception handling in C++ is used to manage runtime errors safely. The keywords try, catch, and throw help prevent program crashes by handling unexpected situations properly.

What is Exception Handling?

Exception handling is a mechanism that detects and handles runtime errors during program execution.

Instead of stopping the program suddenly, exceptions allow errors to be handled safely.

Why Exception Handling is Important

Exception handling is important because it:

  • Prevents program crashes
  • Handles runtime errors safely
  • Improves program reliability
  • Makes debugging easier
  • Improves user experience

Keywords Used in Exception Handling

KeywordPurpose
tryContains code that may generate an error
throwGenerates an exception
catchHandles the exception

Syntax of try, catch, throw

try {

// code that may cause error

throw exception;
}

catch (type variable) {

// code to handle error
}

Basic Example of Exception Handling

#include <iostream>
using namespace std;

int main() {

int a = 10, b = 0;

try {

if (b == 0) {

throw "Division by zero error";
}

cout << a / b;
}

catch (const char* msg) {

cout << msg;
}

return 0;
}

Output

Division by zero error

How Exception Handling Works

  1. Code inside try block executes
  2. Error condition is detected
  3. throw sends exception
  4. Control moves to catch block
  5. Error is handled safely

Example with Integer Exception

#include <iostream>
using namespace std;

int main() {

try {

throw 100;
}

catch (int x) {

cout << "Exception caught: " << x;
}

return 0;
}

Output

Exception caught: 100

Multiple catch Blocks

C++ allows handling different exception types using multiple catch blocks.

Example

#include <iostream>
using namespace std;

int main() {

try {

throw 10;
}

catch (int x) {

cout << "Integer exception: " << x << endl;
}

catch (...) {

cout << "Unknown exception";
}

return 0;
}

Output

Integer exception: 10

Example with User Input

#include <iostream>
using namespace std;

int main() {

int age;

cout << "Enter age: ";

cin >> age;

try {

if (age < 18) {

throw age;
}

cout << "Access granted";

}

catch (int a) {

cout << "Access denied. Age: " << a;
}

return 0;
}

Sample Output

Enter age: 15
Access denied. Age: 15

Standard Exception Handling

C++ provides standard exceptions through the <exception> library.

Example

#include <iostream>
#include <exception>
using namespace std;

int main() {

try {

throw runtime_error("Runtime error occurred");
}

catch (exception &e) {

cout << e.what();
}

return 0;
}

Output

Runtime error occurred

Important Points About Exception Handling

  • try block contains risky code
  • throw generates exception
  • catch handles exception
  • Multiple exceptions can be handled
  • Program continues safely after handling

Common Runtime Errors Handled by Exceptions

  • Division by zero
  • Invalid input
  • File handling errors
  • Memory allocation failures
  • Array out-of-bounds access

Best Practices for Exception Handling

  • Use exceptions only for unexpected errors
  • Write clear error messages
  • Catch specific exceptions when possible
  • Avoid unnecessary exception handling
  • Keep try blocks small

Real-Life Example

Think of a traffic system:

  • Problem occurs on road
  • Traffic police handle situation
  • Traffic continues safely

This is similar to exception handling in programming.

Why try, catch, throw are Important

These keywords are important because they:

  • Improve software stability
  • Prevent sudden crashes
  • Make programs safer
  • Help manage runtime errors efficiently

Applications of Exception Handling

Exception handling is widely used in:

  • Banking software
  • Web applications
  • Database systems
  • File management systems
  • Enterprise applications

Conclusion

The try, catch, and throw keywords in C++ provide a powerful way to handle runtime errors safely. Exception handling improves program reliability, prevents crashes, and helps create stable and professional applications.

Home » Advanced C++ > Exception Handling > try, catch, throw