Exceptions problem

Hi, i wrote this code and i can't understand why it doesn't work. Compilation works fine, with no errors. When i run it, it derails action. Can anyone help me, please?

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

using namespace std;

float func(int, int);
float * quotient(int, int, int);

int _tmain(int argc, _TCHAR* argv[])
{
	float * k;
	try
	{
		k=quotient(8, -1, 5);
	}
	catch(...)
	{
		cerr<<"error"<<endl;
		system("pause");
		return -1;
	}
	
	system("pause");
	return 0;
}

float func(int first, int second)
{
	return first/second;
}
	
float * quotient(int dividend, int divider_from, int divider_to)
{
	float * results=new float [divider_to-divider_from];
	int i=0;
	for(int dz=divider_to; dz>divider_from; dz--)
	{
		if(dz==0)
		{
			cout<<"deallocate memory"<<endl;
			delete [] results;
			throw;
		}
		results[i++]=func(dividend,dz);
	}

	return results;
}
You have to throw something, preferably an std::exception (or a deriver class).
Example throw std::exception;

Simply throw; re-throws the last-exception. You should use it in a catch block, if that catch block can't handle the caught exception.
Last edited on
Thank you :). Simply throw 1; and catch(int nr){} make, that all works fine. I though there's something wrong with delete command. Once again, rly appreciate it.
Topic archived. No new replies allowed.