• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

C++ Exception Handling

Exception Handling

Exceptions are unusual conditions in a program.

They may cause the programs to fail or may lead to errors. They must be deal with.

There are 2 types of exceptions in C++

synchronous and asynchronous

“out-of- range index” and “over –flow” are synchronous type.

Errors caused by the events beyond the central of the program like keyboard interrupts are called asynchronous interrupts.

Exception is a logical error

Exception is an error which occur during runtime (Because Invalid input)


Exceptions Example in C++

# include <iostream.h>

int main()


int n1,n2;

cout<<”Input any 2nos”;

cin >>n1>>n2;




try catch &throw (not recognized turbo c++3.0)


Purpose of  C++ Exception  Handling

The purpose of exceptional handling in C++  is to defect and report an “exceptional circumstance.

Then appropriate action can be taken.

The error handling code basically consists of two segments.

One to detect errors and to throw exceptions.

The other to catch the exceptions and to take appropriate action.


Structure of Try block in C++

This block contain code which has to be monitored for exception handling.

This block contain condition or business block




throw exception-type;


Structure of Catch block in C++

This block contain error logic.

Exception thrown by try block handled by using catch block.

catch (exception-type var)





Example of C++ Exception Handling

# include <iostream.h>

void main()


int n1,n2,n3;


cout<<”\n Input any two numbers”;

cin >> n1>>n2;



if (n2==0)

throw 0; (is type of int)



catch (int x)


cout<<”\n cannot divide number with zero”;




Example of Exception Handling in C++

# include <iostream.h>

# include<conio.h>

# include<string.h>

class login-exception



void Main()


char uname[10],password [10];

login –exception invalid –login;

cout<<”\n Input usernamr”;


cin >>password;



if (strcmp (uname,”nit”)==0&& strcmp (password ,”nit”);

cout <<”\n Welcome to my app”;


throw invalid _login;


catch(login_exception e)


cout<<”\n Invalid username or password”;


Review Date
Reviewed Item
C++ Exception Handling
Author Rating

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on C++ Exception Handling"

    Leave a Message

    Your email address will not be published. Required fields are marked *

    Site Disclaimer, Copyright © 2016 - All Rights Reserved.


    Please leave a message and we'll get back to you soon.

    I agree to be contacted via e-mail.