25 Nov 2019

  • November 25, 2019
  • Amitraj
Exception Handling in C++

-> We often come across some peculiar problems other than logic or syntax error. They are known as exceptions.

-> Exceptions are run time anomalies(errors) or unusual conditions that A program may encounter while executing.

-> Anomalies might include conditions such as division by zero, access to an outside its bounds, or running out of memory or disk space.

-> ANSI C++ provides built in language features to detect and handle exceptions which are basically run time errors.

-> Note that, Exception handling was not a part of the original C++. it is a new feature added to ANSI C++. 



// Exception Handing program in C++.

#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<"Enter two numbers:";
cin>>a>>b;
try
{
if(b!=0)
{
c=a/b;
cout<<c;
}
else
        throw b;
}
catch(int i)
{
cout<<"Exception caught: Division by Zero\n";
}
return 0;
}


OUTPUT:

Enter two numbers:14   0

Exception caught: Division by Zero




Translate

Popular Posts