Exceptional handling simple program

Write a simple program using function that throws an exception if divide by zero occur
and catch the exception in main.
b. Write a program to throw and catch the following type of exceptions:
1. Integer
2. Float
3. String
4. Character

Write one try block and appropriate specific catch block/s.

Hye guys! I am not getting the question. What have string and character to do with this?Do i have to do all of this in main?
Can i catch the exception in main if if send it from a function. I read that catch block is right below the try block.
Last edited on
> Can i catch the exception in main if if send it from a function.
that's the idea, you throw an exception because you don't know how to handle it, but perhaps your caller does.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int main() try{
   //these functions may trhow an exception
   foo();
   bar();
   asdf();
}
catch(int n){
   //...
}
catch(float f){
   //...
}
catch(std::string &s){
   //...
}
catch(char c){
   //...
}
catch(...){
   //...
}

void bar(){
   throw 42;
}
Is there any way to make this work! line 16,20 and 24 are wrong!If the user mistakenly enters, string , float or character then the respective catch block should work!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
using namespace std;
int main() 
{   int a, b, divide;
    cout << "Enter numberator: " << endl;
	cin >> a;
	cout << "Enter denominator: " << endl;
	cin >> b;
	try
	{
		if (b == 0)
		{
			throw 1;

		}
		if(b == "float")
		{
			throw 2.5;
		}
		if (b == "string")
		{
			throw string("apple");
		}
		if (b == 'c')
		{
			throw char('z');
		}
	}
	catch (int n)
	{
		cout << "Infinity" << endl;
	}
	catch (float f)
	{
		cout << "You entered float number" << endl;
 	}
	catch (std :: string &s) 
	{
		cout << "You entered string" << endl;
	}
	catch (char c)
	{
		cout << "You entered a character" << endl;
	}
	system("pause");
}
Last edited on
A friend of mine is telling me that there is function (cin.fail) which returns zero if integer is not entered! Something like that!
Write a simple program using function that throws an exception if divide by zero occur

Your code has nothing to do with your exercise requirements.

1)
Do start by writing one function which checks two numbers before performing a division with them.
- In case the divisor is zero, the function must throw.
- Otherwise, it must return the result.

Call that function from main() and catch its exception.

End of first exercise.


2)
Write 4 functions. Each of them must throw an exception of a specific type (integer, float, string, character...).

Call them from main() and catch their exceptions.

Please note: the exercise doesn’t say those functions should do something canny: they just need to throw.
(Anyway, if they do something canny, maybe you get an higher mark - I don’t know your teacher.)

End of exercise two.


A friend of mine is telling me that there is function (cin.fail) which returns zero if integer is not entered!

Awesome. And… ?
Anyway, here it is: http://www.cplusplus.com/reference/ios/ios/fail/


- - -
Aside note: when you are required to use a floating point type, it doesn’t mean you need to use exactly ‘float’.
Whatever I am throwing , its catching an interger
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
using namespace std;
void integer()
{
	throw 1;
}

void floating()
{
	throw 1.5;
}
void stringvalue()
{
	throw "salman";
}
void character()
{
	throw 'a';
}
int main()
{
	int z;
	try
	{
		cout << "Which exception you want to throw:" << endl;
		cout << "1) integer 2) float 3) string 4) character" << endl;
		cin >> z;
		if (z = 1)
		{
			integer();

		}
		if (z = 2)
		{
			floating();

		}
		if (z = 3)
		{
			stringvalue();

		}
		if (z = 4)
		{
			character();

		}
	}
	catch (int n)
	{
		cout << "Caught an integer exception" << endl;
	}
	catch (float f)
	{
		cout << "Caught a float exception" << endl;
	}
	catch (std::string &s)
	{
		cout << "Caught a string exception" << endl;
	}
	catch (char c)
	{
		cout << "Caught a character exception" << endl;
	}

	system("pause");
}
Bump up the warning levels.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ g++ -g -Wall -Wextra -std=c++11 foo.cpp
foo.cpp: In function ‘int main()’:
foo.cpp:28:12: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   if (z = 1)
            ^
foo.cpp:33:12: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   if (z = 2)
            ^
foo.cpp:38:12: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   if (z = 3)
            ^
foo.cpp:43:12: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   if (z = 4)
            ^

You're using = where you should be using ==
Whatever I am throwing , its catching an interger

Besides the mistakes salem c pointed out, there are other issues in your code.
To make it work, you should start by listening to what people tell you.

This function throws a double:
1
2
3
4
void floating()
{
    throw 1.5;
}


This block deals with floats
1
2
3
4
catch (float f)
{
    cout << "Caught a float exception" << endl;
}


And:
This function throws a const char*:
1
2
3
4
void stringvalue()
{
    throw "salman";
}


This block deals with std::strings
1
2
3
4
catch (std::string &s)
{
    cout << "Caught a string exception" << endl;
}

Thanks a lot Enoizat!
Topic archived. No new replies allowed.