VINOTH ENERGETIC
VINOTH ENERGETIC

Reputation: 1843

What is the purpose of using throw after function signature in c++?

As per I understood, exception in c++ will be call explicitly by using throw statement

 void exp()
 {
    try{    
       int a,b=0;
       if(b==0)
       {
          throw b; // called explicity
       }
       a=10/b;
    }
    catch(...)
    {

       //   cout<<"Exception called\n"<<e;
    }    
 }

Given the following snippet:

void myfunction(.............)throw(SQLEXCEPTION)
{
    //my stuff
}
  1. What is the purpose of using throw after function signature.
  2. Where catch block can be defined for such throw.
  3. Will it handle the exception implicitly.
  4. If not handle implicity, at which situations the throw execute.

Upvotes: 2

Views: 2570

Answers (4)

ichramm
ichramm

Reputation: 6632

throw() in the function signature specifies the exceptions a function may throw.

Example:

void foo() throw(std::runtime_error, std::logic_error);

throw() with no arguments means the function throws no exceptions

Exception specification is deprecated and its usage is not recommended.

Upvotes: 4

AlexDan
AlexDan

Reputation: 3331

1- The use of throw(type-list) after the function signature is so that you or the programmer who will read your code will know that this function can only throw the types that you specifed in type-list.

void f()throw(){};// This function can not throw any object of any type.
void f()throw(A){}; // you can throw any A object + 
                   // any object of a type drived from A if A is a User-defined Type

If you throw an object of a type that is not in the exception specification, std::unexpected() function will be called.

2- There is no catch block here. the throw(type-list) after function signature is just insurance that this function wont throw any type except objects of type in type-list.

What you are using is called Exception specification, one example of it's use is like this:

    int f(int x)throw(int){
             if (x==0) throw 0;
              return x;
    }

    int main(){
    try{
        f(0);  
       }
    catch(int e){
     // something
       }
    }

You can remember two things:

i) Don't use exception specification since they are deprecated.
ii) If you have to use them, then you must now that some compilers will refuse to inline a function just because it has an exception specification.


Update:

void f()throw(int){
      try{
       // code goes here ...
       throw 1.1; // note this is throwing a float
      }
      catch(float){
      // exception handled
      }

  }

In this example you wont get any errors, even if you throw an exception of type float, which is not specified in the function exception specification which only allow you to throw object of type int. This is because you handled the exception inside the function.


void f()throw(int){
        throw 1;

 }
 int main(){
       try{
           f();

       }
       catch(int){// exception handled}

 }

You should always put a function that you think may throw an exception inside a try-block, the code above will get executed fine since f() throw an int and it got handled.


void f()throw(int){

   throw 1.1; // note this is throwing a float

 }

 int main(){
    try{ 
       f();

    }
  catch(...){//}
 }

This will result in function call of std::unexpected() because your function f() throw an exception of type float which is not in the function exception specification.

Upvotes: 2

Remy Lebeau
Remy Lebeau

Reputation: 595742

A throw() specification on a function declaration indicates which specific exception(s) the function is allowed to throw to the caller. It is a kind of contract so the caller knows what to expect. If no throw() is specified, the function is allowed to throw any exception. If the throw() is empty, the function is not allowed to throw any exceptions. So, in your example, myfunction() is allowed to throw only SQLEXCEPTION and derived exceptions.

Upvotes: 4

mikek3332002
mikek3332002

Reputation: 3562

The purpose of the throw is that the programmer is creating an exceptions.

For example you could have a custom array object that checks to see if it is in bounds and if it is not then throw an exception.

Upvotes: -3

Related Questions