Try-Catch Error

It gives a weird error (according to me).
It says I didn't associate a try block with the catch handler, I tried already a few things, but the errors kept appearing when I compiled the programm. Anyone who knows how to fix this?
This is the code:

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <windows.h>

using namespace std;

struct id {
	int Year;
	int Day;
	int Month;
	int Brother;
	int Sister;
	int BroSis;
};

int main(){
	
	id first;
	int a;
	cout<<"Hello!\n";
previous:
	try{
		cout<<"On which day of the month are you born?\n";
		cin>>first.Day;
		if ((first.Day > 31) | (first.Day < 1))
			throw(1);
		cout<<"In what month are you born? \n";
		cin>>first.Month;
		if ((first.Month < 1) | (first.Month > 12))
			throw(2);
		cout<<"In what year are you born? \n";
		cin>>first.Year;
		if (first.Year < 1900)
			throw(3);
		cout<<"How many brothers/sisters do you have?\n";
		cin>>first.BroSis;
		if (first.BroSis < 0)
			throw(4);
		cout<<"How many brothers do you have?\n";
		cin>>first.Brother;
		if ((first.Brother > first.BroSis) | (first.Brother < 0))
			throw(5);
	
		catch(int a)
		{
			cout<<"Error occurred: ";
			if(a==1)
			{
				cout<<"This day isn't recognized (numbers only allowed).\n";
				goto previous;
			}
			else if(a==2)
			{
				cout<<"This month isn't recognized (numbers only allowed).\n";
				goto previous;
			}
			else if(a==3)
			{
				cout<<"Invalid year entered (numbers only allowed).\n";
				goto previous;
			}
			else if(a==4)
			{
				cout<<"Invalid number of brothers/sisters entered (numbers only allowed).\n";
				goto previous;
			}
			else if(a==5)
			{
				cout<<"Invalid number of brothers entered (numbers only allowed).\n";
				goto previous;
			}
			else
			{
				cout<<"Unknown error.\n";
				Sleep(500);
				return 1;
			}
		}

	first.Sister = first.BroSis - first.Brother;



	ofstream file ("ID.txt");
	string line;
	char exit;
		
	if (file.is_open())
	{
		file<<"You are born on "<<first.Day<<"-"<<first.Month<<"-"<<first.Year<< ".\n";
		file<<"You have "<<first.Brother<<" brothers and "<<first.Sister<<" sisters.\n";
		file.close();
		ifstream file ("ID.txt");
		while(! file.eof())
		{
			getline (file,line);
			cout<<line<<endl;
		}
		file.close();
	}
	else cout<<"Unable to open file\n";

next:
	cout<<"Do you want to continue? Y/N\n";
	cin>>exit;
	if (exit == 'N'){
		cout<<"Exiting...\n";
		Sleep(500);
		return 0;
	}
	else if (exit == 'Y')
		goto previous;
	else
		cout<<"Invalid Entry\n";
		goto next;
}


These are the errors (compiling included):

1>------ Build started: Project: C++ - Experimental, Configuration: Debug Win32 ------
1>Compiling...
1>main.cpp
1>c:\users\t\documents\visual studio 2008\projects\c++ - experimental\c++ - experimental\main.cpp(46) : error C2318: no try block associated with this catch handler
1>c:\users\t\documents\visual studio 2008\projects\c++ - experimental\c++ - experimental\main.cpp(119) : error C2317: 'try' block starting on line '24' has no catch handlers
1>c:\users\t\documents\visual studio 2008\projects\c++ - experimental\c++ - experimental\main.cpp(119) : fatal error C1075: end of file found before the left brace '{' at 'c:\users\thijs\documents\visual studio 2008\projects\c++ - experimental\c++ - experimental\main.cpp(18)' was matched
1>Build log was saved at "file://c:\Users\Thijs\Documents\Visual Studio 2008\Projects\C++ - Experimental\C++ - Experimental\Debug\BuildLog.htm"
1>C++ - Experimental - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Thanks =)
You didn't close the try brace before catch
Oh dear goto...
Lol, yeah I'm noob, but I don't like for and while loops =]
You really need to change that attitude. You're actually the first person I've ever heard say that lol but I would change if I were, they are better.
Lol, yeah I'm noob, but I don't like for and while loops =]


What do you mean you don't like for and while loops? If you plan to make a career out of programming or want to make anything remotely useful you'll start using them!
if you want to use things like "goto" learn to write assembly code. Even in assembly the structure of that example would get an F rating.
goto.... I can't write any more, I'm about to vomit...
Lawl, guys, I'm programming (in C++) for 2 weeks now, I replaced it already, but you can't blame me for not making 10 while loops in one stupid thingy...
We are trying to teach something.
You are free to use as many gotoes as you want
But in most cases you have things which are better than a goto
Anyway, how is making 10 whiles worse than 10 gotoes?
10 whiles would veritably own 10 gotos. At least you can see where the while is going. Goto is so impossible to use correctly that, in general, you'd be better off assuming it's not there. And that is not even close to an acceptable use of goto. So yeah, I'd blame you if you used goto again.
10 while loops would be better than 10 goto statements. The beauty of cin is that it can tell you from its return value whether it worked. You can encapsulate an error test and the range test within the while loop. Better yet, you could write a common function for the I/O where you can test the validity of the operation as well as the range and have only one while loop in that. Take a look at these FAQs and see if you can improve the program with that knowledge. Take a look at items 15.2-15.4.
http://www.parashift.com/c++-faq-lite/input-output.html
Topic archived. No new replies allowed.