try catch ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
J_Iceman05
Forum Commoner
Posts: 72
Joined: Wed Aug 03, 2005 10:52 am
Location: Las Vegas, NV

try catch ?

Post by J_Iceman05 »

I know in C there is the try/catch functions. I have been searching and appearently there is the same basic ability it PHP aswell. But I can't seem to get mine to work. It stops everything from working.
My code is as follows

Code: Select all

<?php
function DecayIt ($origActive, $halfLife, $origTime, $origDate, $currTime, $currDate) {

  $origActive = floatval($origActive);
  $halfLife = floatval($halfLife);
  $origTime = TDateTime($origTime, 1);
  $origDate = TDateTime($origDate, 0);
  $currTime = TDateTime($currTime, 1);
  $currDate = TDateTime($currDate, 0);

  $currentActivity = 0.0;
  $nCurrentActivity = 0.0;
  $fExp = 0.0;
  $fExp1 = 0.0;
  $fMult = 0.0;
  
  $elapsedTime = $currTime + $currDate;
  
  if ($elapsedTime == 0.0) {
    return 0.0;
  }

  $elapsedTime = ($currTime + $currDate) - ($origTime + $origDate);
  $elapsedTime = $elapsedTime * 24.0;
  
  if ($elapsedTime != 0.0) {
    $elapsedTime = floatval(number_format($elapsedTime, 3, '.', ''));
  } else {
    return $origActivity;
  }
  
  if ($halfLife > 0) {
    $fMult = -0.693 * $elapsedTime;

    try 
    {
      $fExp = $fMult / $halfLife;
      if ($fExp >= 11356.50) {
        $fExp1 = 0.0;
      } else {
        try 
	{
          $fExp1 = exp($fExp); 
	}
       	catch ($e) 
	{ 
          $fExp1 = 0.0; 
	}
      }
      $nCurrentActivity = $origActivity * $fExp1;
    }
    catch($e)
    {
      $nCurrentActivity = 0.0;
    }
  } else {
    $nCurrentActivity = 0.0;
  }
  
  $currentActivity = $nCurrentActivity;

  try
  {
    if ($currentActivity == 0) {
      $currentActivity = 0.0;
    }
  }
  catch ($e)
  {
    $currentActivity = 0.0;
  }

  return($currentActivity); 
}


// this is just to test, not used in the real page

echo 'hi<br><br><br>'; // prints hi
$time = TDateTime('12:34', 1);
echo $time.'<br>';   // prints 0.52361111111
$day = TDateTime('10-12-2005', 0);
echo $day.'<br>';    // prints 38637

i only see the text if i comment every try / catch
what do i have wrong with the try / catch portion. is there extra functions needed for it to work? or is there something else i need to do?

Thanks for you help.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

I believe try/catch was introduced in php 5 so if you're using an older version it wont' work.
User avatar
J_Iceman05
Forum Commoner
Posts: 72
Joined: Wed Aug 03, 2005 10:52 am
Location: Las Vegas, NV

Post by J_Iceman05 »

... ok. That sucks for me. ::slap in the face:: oh well. Thank you very much for your help
I'll either have to upgrade or find another way around it.

If anyone has any good ideas on how to imitate that process, your input would be much appreciated ^^

Thanks again.
User avatar
chrys
Forum Contributor
Posts: 118
Joined: Tue Oct 04, 2005 9:41 am
Location: West Roxbury, MA (Boston)

Post by chrys »

Couldn't you just use

if() {} else{} ?
User avatar
J_Iceman05
Forum Commoner
Posts: 72
Joined: Wed Aug 03, 2005 10:52 am
Location: Las Vegas, NV

Post by J_Iceman05 »

with the if () {} else {} i dont think you can have it set up like...

Code: Select all

if
    (                                         // needs to test from here
      $fExp = $fMult / $halfLife;
      if ($fExp >= 11356.50) {
        $fExp1 = 0.0;
      } else {
        if 
	{
          $fExp1 = exp($fExp); 
	}
       	else 
	{ 
          $fExp1 = 0.0; 
	}
      }
      $nCurrentActivity = $origActivity * $fExp1;
    ) {}                                  // to here
    else
    {
      $nCurrentActivity = 0.0;             // if fail, do this
    }
unless I am mistaken, then i need another solution. but if you can, then that would work perfectly

I know that the way I have it set up in my code does not work. How would I have to set it up for it to work (if it's possible)?
User avatar
chrys
Forum Contributor
Posts: 118
Joined: Tue Oct 04, 2005 9:41 am
Location: West Roxbury, MA (Boston)

Post by chrys »

Yes, you can pretty much do anything with if/else. YOu will need to slightly restructure your logic, but I assure you it's possible. If you spell out the logic to me in english, I can tackle it for you.
User avatar
J_Iceman05
Forum Commoner
Posts: 72
Joined: Wed Aug 03, 2005 10:52 am
Location: Las Vegas, NV

Post by J_Iceman05 »

If you refer the code in my first post, it is to calculate the half life (of a radioactive product) from a starting time to an end time. This may not neccessarily the full half life, thus the need for the calculation.
Example: productA's half-life = 2 years. you need 40 units 2 weeks from now. how much do you need to make now in-order to have exactly 40 in 2 weeks?

fairly common calculation. I am actually trying to convert it from C++ in to PHP

Code in C++

Code: Select all

float DecayIt(float origActivity, float halfLife,
              TDateTime origTime, TDateTime origDate,
              TDateTime currTime, TDateTime currDate)
{
    float currentActivity = 0.0;
    long double nCurrentActivity = 0.0;
    double fExp = 0.0;
    long double fExp1 = 0.0;
    float fMult = 0.0;

    TDateTime elapsedTime;
    elapsedTime = currTime + currDate;

    if(double(elapsedTime) == 0.0)
    {
        return 0.0;
    }

    elapsedTime = (((currTime + currDate) - (origTime + origDate)));
    elapsedTime = double(elapsedTime) * 24.0;
    if (double (elapsedTime) != 0.0)
        elapsedTime = double (StrToFloat(FormatFloat("#0.00#", elapsedTime)));
    else
        return origActivity;
    if (halfLife > 0)
    {
        fMult = -0.693 * elapsedTime;

        try
        {
            fExp = fMult / halfLife;
            if (fExp >= 11356.50)
            {
                fExp1 = 0.0;
            }
            else
            {
                try { fExp1 = expl(fExp); } catch (...) { fExp1 = 0.0; }
            }
            nCurrentActivity = origActivity * fExp1;
        }
        catch( ... )
        {
            nCurrentActivity = 0.0;
        }
    }
    else
        nCurrentActivity = 0.0;
        
    currentActivity = (float) nCurrentActivity;

    try
    {
        if (currentActivity == 0)
            currentActivity = 0.0;
    }
    catch (...)
    {
        currentActivity = 0.0;
    }

    return(currentActivity);
}
The try/catch is used because many unexpected errors may occurs.
and the catch should be executed if any errors do occur while processing the try
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

if it's just math, you could probably write something in JS to do it (and use try/catch).

if there's a possibility of it throwing errors, I'm not sure that if/else will suffice as it will only return booleans and errors don't fall within a boolean 8O

I'd either upgrade php to 5 or do it with JS, and upon further reflection as I write this sentence, I'd do it with JS 8)
User avatar
J_Iceman05
Forum Commoner
Posts: 72
Joined: Wed Aug 03, 2005 10:52 am
Location: Las Vegas, NV

Post by J_Iceman05 »

I have been trying to do it in javascript, and it has been much more challenging that I thought. I don't know java nearly as well as i know PHP; and as far as I'm concerned, I don't know PHP all that well either, so that is saying a lot. But I have been working at it, and trying different functions to imitate some of those in php [such as explode() and the Date('U', $time) function]
If anyone knows of any good websites that gives a list of javascript commands (ie. http://www.php.net for php) That would be wonderful. everything i have found so far either gives a small portion of a few commands/topics or ellaborates in greate detail on a single command/topic, such as parseFloat().
as always, Thanks for you input and help.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

I don't know of any good resources like php.net for js.

probably the best thign to do would be to get started, then post any questions you have in the client side forum on this site.

I'm sure we could work through it with you.
User avatar
J_Iceman05
Forum Commoner
Posts: 72
Joined: Wed Aug 03, 2005 10:52 am
Location: Las Vegas, NV

Post by J_Iceman05 »

That is a good idea, I think i will. Thank you very much.
You will probably see a post from on that section by the end of today.
Hopefull i can get what I need without doing so.
But I do appreciate the support.
Thank you everyone.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

DevGuru has a pretty good list of the functions and things.. You could always pull up the specification for the language as well: http://www.ecma-international.org/publi ... ma-262.htm
Post Reply