Home C++ Introduction Decisions Loops Input/Output Functions Stack and Heap References Arrays Searching and Sorting Recursion Pointers Character and Strings Structures Classes Inheritance Exceptions Templatess STL Modern C++ Misc Books ----


Exceptions


Contents

Type Conversion

We need to be careful when stating the type of object in the "catch" handler. There is no implicit conversion performed. The exact type that was specified in the throw statement must be used. Let's take a look at an example that demonstrates implicit conversion.

File: implict1.cpp
class MyClass
{
public:
    MyClass(int value)
    { /* ... */
    } // Implicit conversion from int to MyClass
};

void func(MyClass obj) { /* ... */ }

int main()
{
    func(10); // Implicitly converts 10 (int) to MyClass
    MyClass obj = 5; // Implicitly converts 5 (int) to MyClass
    return 0;
}


    func(10); // Implicitly converts 10 (int) to MyClass
    MyClass obj = 5; // Implicitly converts 5 (int) to MyClass

For the "func(10)" the compiler see that "func" takes a "MyClass"
object and also sees that "MyClass" has a constructor that takes
an integer and decides to initialize "obj" using this
constructor. Same logic is used in the statement "MyClass obj = 5;" .
We can direct the class not to use implicit conversion in the following
program.



File: implict2.cpp
class MyClass
{
public:
  explicit MyClass(int value)
    { /* ... */
    } // Implicit conversion from int to MyClass
};

void func(MyClass obj) { /* ... */ }

int main()
{
    func(10); // Implicitly converts 10 (int) to MyClass
    MyClass obj = 5; // Implicitly converts 5 (int) to MyClass
    return 0;
}

$ g++ implicit2.cpp ; ./a.exe
implicit2.cpp: In function ‘int main()’:
implicit2.cpp:13:10: error: could not convert ‘10’ from ‘int’ to ‘MyClass’
   13 |     func(10); // Implicitly converts 10 (int) to MyClass
      |          ^~
      |          |
      |          int
implicit2.cpp:14:19: error: conversion from ‘int’ to non-scalar type ‘MyClass’ requested
   14 |     MyClass obj = 5; // Implicitly converts 5 (int) to MyClass
      |                   ^



File: construct1.cpp
#include <iostream>
#include <fstream>
#include <string>

using namespace std;


int divideOperation2( int x1, int y1 )
{
    if ( y1 == 0 )
      {
         cout << "Step1" << endl ;
         throw  "Divide by zero." ;
         //throw  String("Divide by zero.") ;
      }
     return ( x1 / y1 ) ;
}

void method1( string str1 )
{
   cout << "Inside method1: " <<  str1 << endl   ;

}


int main()
{


        try
         {
              string str1 = "Divide by zero." ;
              method1( str1 ) ;
              const char* str2 =  "Divide by zero str2."  ;
              method1( str2 ) ;
              string str3 = str2 ;

              //Also allowed
              string str4( str2 ) ;

              divideOperation2 ( 4 ,  0 ) ;
         }
        catch ( const string& str1 )
        //catch ( const char* str1 )
        {
            cout << "Step2" << endl ;
            cout << str1  << endl ;
        }
        cout << "Step3" << endl ;

    return 0;
}

$ g++ construct1.cpp ; ./a.exe
Inside method1: Divide by zero.
Inside method1: Divide by zero str2.
Step1

The below code shows how a "const char*" can be implicitly converted to
a string type. However the handler that contains a string object does not get
called because the "const char*" is not implicitly converted to a string.

			  string str1 = "Divide by zero." ;
			  method1( str1 ) ;
			  const char* str2 =  "Divide by zero str2."  ;
			  method1( str2 ) ;

Exercise

1) The below program terminates. Correct it so that the catch handler is properly called.

File: type_ex1.cpp
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class A
{
    public:
     A( int x1 ) {}
};


int main()
{


        try
         {
             throw( 1 ) ;
         }
      catch ( const A& obj1A )
        {
        }


    return 0;
}