Try-Catch (Catching Infinite Result)

I'm attempting to use a "try-catch" construct to "try" to calculate various equations in a numerical integration, then "catch" the result if it exceeds the defined numerical limits of a "double" variable.

Does anyone have a recommendation relative to how to structure this?

Using:

try { } catch (...){ }

(with the equations inside the brackets of the "try")
doesn't seem to work. The error seems to simply force the logic to cease to proceed, so it exits the function entirely, without proceeding to the end.

Thank you.
I'm not sure what your trying to do. Would be good if you could supply some code.

Regardless though, Try/Catch is for error handling. You would put your code in the Try block and in the Catch block you put code to execute if the stuff in the Try block fails.

Sort of like that:

1
2
3
4
5
6
7
8
9
try
{
    // your code
}
catch
{
    // code to process if the code in the try block failed.
    std::cout << "Code failed!" << std::endl;
}
Does this work for you?

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
#include <iostream>
#include <stdexcept>
#include <limits>

double func(double value )
{
    double result(value) ;

    while ( --value > 0 )
    {
        result *= value ;

        if ( result == std::numeric_limits<double>::infinity() )
            throw std::domain_error("infinity") ;
    }

    return result ;
}

int main()
{
    try {
        func(1000000) ;
    }
    catch( std::exception& ex )
    {
        std::cout << "Exeception caught: " << ex.what() << '\n';
    }
}


Thanks!

I need to adapt the preceding to my needs. The compiler isn't happy.

(The compiler complains about my use of "infinity()" in the catch statement to try to trigger the logic flow to redirect to a catch statement to reduce the time step.)

I didn't load "iostream.h", but I did add the other two.

My code's content is:

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
try
		{
		/*Begin implementation of Fourth Order Runge-Kutta 
		approximation to expansion
		*/
		//
		//Runge-Kutta Approximation for Vtt
		K_vtt1= Vtt + (4.*3.1415*G*((-Dens_Matter + 2*Dens_Lambda)+3*(-Dens_Matter+2*Dens_Lambda)*pow((Rtt*delta_t)/(300.E6),2))*Vol_uni);
		K_vtt2= Vtt + K_vtt1 / 2.;
		K_vtt3= Vtt + K_vtt2 / 2.;
		K_vtt4= Vtt + K_vtt3;
		Vtt = Vtt + K_vtt1 / 6. + K_vtt2 / 3. + K_vtt3 / 3. + K_vtt4 / 6.;
		//End of Runge-Kutta Approximation for Vtt
		//
		//Runge-Kutta Approximation for Vt
		K_vt1= Vt + Vtt*delta_t;
		K_vt2= Vt + K_vt1 / 2.;
		K_vt3= Vt + K_vt2 / 2.;
		K_vt4= Vt + K_vt3;
		Vt = Vt + K_vt1 / 6. + K_vt2 / 3. + K_vt3 / 3. + K_vt4 / 6.;
		//
		//End of Runge-Kutta Approximation for Vt
		Vol_uni = Vol_uni + Vt*delta_t + 0.5*Vtt*pow(delta_t,2.);
		if (Vol_uni < 0.) Vol_uni=0.;
		Runi =pow(Vol_uni/(4./3.*3.1415),double(1./3.));
		Rtt = Vtt/Vol_uni*Runi/3.;
		if (Explode == false)delta_t = sqrt(abs(Runi/(2.*Rtt)))/100000.;
		if (delta_t > 1000&&Explode == false) delta_t = 1000;
		/*End approximation to expansion
		Selection of years in which to store data follows.*/
			if (((int)(year*10./1.E9)==1&&count<1)||((int)(year/1.E9)==2.&&count<2)||((int)(year/1.E9)==5&&count<3)||((int)(year/1.E9)==7&&count<4)||((int)(year/1.E9)==10&&count<5)||((int)(year/1.E9)==12&&count<6)||((int)(year/1.E9)==15&&count<7))
			{
			/*This segment merely selects the values to be saved in the output file
			in a manner to be consistent with that used to produce spreadsheet
			graphs via an alternate model.*/
			Universe[count].year =year;
			Universe[count].V = Vol_uni;
			count = count++;
			}
		year = year + delta_t/(60*60*24*365.25);
		Dens_Matter = Mass_Common/Vol_uni;
		Dens_Lambda = (Mass_Common * Omega_lambda/Omega_matter)/Vol_uni;
		}
		//End of try statement, beginning of catch statement continuation
		catch (std::numeric_limits<double>::infinity())
		{
		Explode=true;
		delta_t = delta_t/2.;
		}
		Explode=false;
		}

		/*Next call routine to write all data stored in structure to CSV file
		to be loaded into spreadsheet via output function "CSV_filewrite".*/
		retval=CSV_filewrite(count, Universe, NameFile);
		//Check for errors writing file.
		if (retval ==-1) 
				{
				/*No specific notification required for error writing file.
				This is responsibility of operating system.*/
				return;
				}
		
/*END OF CODE TO COMPUTE VALUES




} */
You don't throw anything in the try block, so there isn't going to be anything to catch.

In catch(x) x must be a type, not a value. Or you could use catch(...) to catch anything thrown.
Last edited on
I need to review this "throw" syntax. I've been away from C++ for decades. My goal is to "try" the equations for K_vtt1, K_vt1, and Vol_uni, then redirect flow to the catch statement to reduce the timestep if any of those computations blow-up, after I've worked out the details regarding how to implement the 4th order Runge-Kutta numerical integration.

With three dots between parentheses after the "catch", I'd expect any error in any line between the "try" brackets to redirect to the catch brackets. It isn't happening. An infinite result in the "try" segment simply redirects the logic out of the while-do loop.

Thank you.
Last edited on
Topic archived. No new replies allowed.