try, catch, throw???

I already seen this link:
http://www.cplusplus.com/doc/tutorial/exceptions/



and it doesn't make a lot of sense to me, since it says "//code here" instead of showing what the code should be.

I want this program to catch letters. so that if someone uses them instead of numbers it will close the program, instead of going into the never ending cycle.


Please edit this code so I can understand.
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
#include "stdafx.h"
#include <Windows.h>
#include <iostream>



int main()
{

	//Usings
	using namespace std;

	//Ints
	int a, b;
	int c;

	//No Char, Strings, Double, Float, Bools


	//Enter in Value of 'A'
	cout << "\n\n\t\t Enter Value For 'A'\n\t\t\t\t====> ";
	cin >> a;
	
	//Enter in Value of 'B'
	cout << "\n\n\t\t Enter Value For 'B'\n\t\t\t\t====> ";
	cin >> b;

	//Declare 'C'
	c=(a+b);


	//Last message
	cout << "\n\n\t\t C = " << c << "\n";
	
	//Used to avoid C++ religious wars
	cin.get();

	//Not a VOID main()
	//return value
	return 0;
}






1
2
3
try
catch
throw





Last edited on
You need to throw an exception when the istream fails. http://www.cplusplus.com/reference/iostream/ios/exceptions/
Then you catch the exception. Inside the catch block you terminate the program
Thanks, but this only helps so much. The reason is that he/she uses
1
2
3
4
Try 
{
       file
}


I'm wondering a few things, but the first is how this should look:
1
2
3
4
5
6
7
8
9
10
11
12
try
{
       cin >> a;
}
//---------
//----OR---
//---------

try
{
       a;
}



I'm not sure of the first step, and all the tutorials (the link you gave included) are talking about more advanced programming, that deal files, etc. I'm just wanting someone to show me the code from my first post, used with the try/catch/throw. So I can have some idea of how to do this in the future.
In a try block, you may write code as you normally would, so the first example might be closer to what you were thinking of.

Does this help you out a bit?

-Albatross
Perhaps, so is this correct?:



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
#include "stdafx.h"
#include <Windows.h>
#include <iostream>



int main()
{


	using namespace std;

	int a, b;
	int c;



        try
{

	cout << "\n\n\t\t Enter Value For 'A'\n\t\t\t\t====> ";
	cin >> a;
	
}


	cout << "\n\n\t\t Enter Value For 'B'\n\t\t\t\t====> ";
	cin >> b;


	c=(a+b);


	cout << "\n\n\t\t C = " << c << "\n";
	
	
	cin.get();

	return 0;
}


Last edited on
Now for the next part, I don't understand.... What an I supposed to catch?
Something like:

1
2
3
4
catch ("a" || "b" || "c")
{
std::cout << "Sorry you can't use letters";
}
Last edited on
1
2
3
4
5
try{
//code
}
//nothing here
catch(
In your code there is nothing that could throw an exception. Use the flags or tested yourself if( !cin>>a ) throw "an exception";

Edit: about what to catch. You catch what you throw. By instance if new fail it throws a bad_alloc exception
1
2
3
4
5
6
7
8
9
10
int *ptr, n=0;
try{
	while(true){
		ptr = new int[10000000];
		n++;
	}
}
catch( std::bad_alloc &){
	std::cout << "Out of memory " << n;
}
If you use the istream exception catch( std::istream::failure )
1
2
3
if(!cin >> a) throw 42;
//...
catch(int)


This one catches everything (that not was already handled) catch(...)
Last edited on
So:

1
2
3
4
5
6
7
8
9
10
11
         try
{

	cout << "\n\n\t\t Enter Value For 'A'\n\t\t\t\t====> ";
	cin >> a;
	
}

if ( !cin>>a)
throw "Can't use letters";


Maybe I'm looking at this the wrong way, or making it really hard, but I didn't think you could use:
if not cin

if ( !cin>>a)
Last edited on
The throw must be inside the try block.
These are equivalent if( !cin>>a ) or if( !(cin>>a) ) or
1
2
cin>>a;
if( !cin.good() )
Last edited on
ne555, I'm thankful you are trying to help, but the code you posted made this harder to grasp. The only thing I understand is that "you must throw something, to catch something". Please keep in mind I'm new. and:

1
2
3
4
5
6
7
8
9
10
int *ptr, n=0;
try{
	while(true){
		ptr = new int[10000000];
		n++;
	}
}
catch( std::bad_alloc &){
	std::cout << "Out of memory " << n;
}


is a bit too much.

I don't understand what anyone is saying, I'm simply asking how to use try/catch/throw with my original code.

/////////////////////////////////////////////


The code:
1
2
3
if(!cin >> a) throw 42;
//...
catch(int)


Softa makes sense, but what if my int is not 42.
Last edited on
1
2
int a;
if( !cin>>a ) throw 42;
You try to read a number. If that fails you throw an exception (in this case the number 42).
Inside the catch block you are sure that something went wrong in your program (in this case corrupted input).

Also check this http://www.cplusplus.com/forum/articles/6046/
Topic archived. No new replies allowed.