---
title: "Exception Handling in C++"  
description: "In this blog, I’m explaining about Exception handling in C++An  exception  is a problem that arises during the execution of a program Exceptions provi"  
author: "priyanka kushwaha"  
published: 2015-02-05  
updated: 2015-02-05  
canonical: https://www.mindstick.com/blog/755/exception-handling-in-c-plus-plus  
category: ".net"  
tags: ["visual c++"]  
reading_time: 1 minute  

---

# Exception Handling in C++

In this blog, I’m explaining about [Exception handling](https://www.mindstick.com/articles/12240/introduction-of-exception-handling) in C++

An exception is a [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) that arises during the [execution](https://www.mindstick.com/blog/178/synchronous-and-asynchronous-command-execution-in-c-sharp-dot-net) of a [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program) [Exceptions](https://www.mindstick.com/interview/22871/define-predifined-generic-exceptions) provide a way to [transfer](https://www.mindstick.com/blog/11733/debt-management-for-startups-consolidation-loan-vs-balance-transfer) [control](https://www.mindstick.com/blog/197/asp-dot-net-repeater-control) from one part of a program to another.

C++ exception handling is built upon three [keywords](https://www.mindstick.com/articles/12899/how-to-spot-if-you-re-optimizing-for-the-wrong-keywords): _try, catch and throw.

**throw**: A program throws an exception when a program show up. This is done using a throw keyword.

**try**: A try block identifies a block of a code for which particular exceptions will be activated. It’s followed by one or more catch blocks.

**catch**: A program catches an exception with an exception handler at the place in a program where you want to handle the problem. This is done using a catch keyword.

##### Example:

```
// Exception.cpp : main project file. #include "stdafx.h"#include <iostream>#include <exception>#include <Windows.h>using namespace std;using namespace System; int main(array<System::String ^> ^args){    _try            {                        int firstNum,secNum,res;                        cout<<"Enter first number";                        cin>>firstNum;                        cout<<"Enter second number";                        cin>>secNum;             if(secNum==0)                throw;                        res=firstNum/secNum;                        cout<<"Result="<<res;            }            _except(EXCEPTION_FLT_DIVIDE_BY_ZERO)            {                        cout<<"InValid data";            }            Console::ReadKey(0);    return 0;}
```

---

Original Source: https://www.mindstick.com/blog/755/exception-handling-in-c-plus-plus

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
