C++ specifying exceptions

C++ specifying exceptions

It is possible to restrict a function to throw only certain specified exceptions. This is achieve by adding a keywords throw list clause to the function definition. The general form for using an exception specification is in below example

type function(arg-list) throw (type list)
{
………
………
………
function body
}

The type list specifies the type of exceptions that may be thrown show in the above program. if Throwing any other type of exceptions in program it will cause abnormal program termination. if we wish to prevent a function from throwing any exception, we may do by making the type-list empty inside arguments. That is we must use

throw(); // Empty list

in the function header line

Note: A function can only be restricted in what types of exceptions it throws back to the try block that called it. The restrictions applies only when throwing an exception out of the function

TESTING THROW RESTRICTIONS
#include < iostream >
using namespace std;
void test(int x) throw(int,double)
{
if(x==0) throw ‘x’; // char
else
if(x==1) throw; // int
else
if(x==-1) throw 1.0; // double
cout<<“End of the function block \n”;
}
void main()
{
try
{
cout << “Testing throw restrictions \n”;
cout << “X==0 \n”;
test(0);
cout << “x==1 \n”;
test(1);
cout << “x==-1 \n;
test(-1);
cout << “x==2 \n”;
test(2);
}
catch(char c)
{
cout << “caught a character \n”;
}
catch(int m)
{
cout << “caught an integer \n”;
}
catch(double d)
{
cout << “caught a double \n”;
}
cout << “End of try catch system \n\n”;
}

Output:
Testing Throw RESTRICTIONS
x==0
caught a character
End of try catch system

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise