Page 1 of 1

try catch in javascript? [SOLVED]

Posted: Mon Oct 24, 2005 1:03 pm
by J_Iceman05
Basically, I'm trying to make a Half-Life calculator. I'm converting it from C++ to javascript. This is my code so far...

Code: Select all

function DecayIt(origActivity, halfLife, origTime, origDate, currTime, currDate)
{
    origActivity = parseFloat(origActivity); 
    halfLife = parseFloat(halfLife);
    origTime = TDateTime(origTime);
    origDate = TDateTime(origDate);
    currTime = TDateTime(currTime);
    currDate = TDateTime(currDate);
    
    var currentActivity = 0.0;
    var nCurrentActivity = 0.0;
    var fExp = 0.0;
    var fExp1 = 0.0;
    var fMult = 0.0;
    
    currentActivity = parseFloat(currentActivity);
    nCurrentActivity = parseFloat(nCurrentActivity);
    fExp = parseFloat(fExp);
    fExp1 = parseFloat(fExp1);
    fMult = parseFloat(fMult);

    var elapsedTime;
    elapsedTime = currTime + currDate;

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

    elapsedTime = (currTime + currDate) - (origTime + origDate);
    elapsedTime = parseFloat(elapsedTime) * 24.0;
    if (parseFloat(elapsedTime) != 0.0) {
        elapsedTime = parseFloat(StrToFloat(FormatFloat(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 = parseFloat(nCurrentActivity);
    try
    {
        if (currentActivity == 0)
            currentActivity = 0.0;
    }
    catch (...)
    {
        currentActivity = 0.0;
    }
    return(currentActivity);
}
The try catch part does not work. If i comment out the try/catch parts. then it wont work correctly. but if they aren't commented out, then no other javascript works. I'm guessing I'm using them incorrectly, but I can't find anything of how to use it in javascript.

Any help on what I should do is much appreaciated. Thank you for any input.

edit: misuse of java inplace of javascript... :oops: sorry for any misunderstanding
d11wtq wrote:Not sure on how to use try/catch in JS but on a side-note: JavaScript is not Java... the two are unrelated and entirely separate. If you have been googling for try/catch in java you wont find javascript realted stuff ;)
sorry about that, i did google for javascript by the way

Posted: Mon Oct 24, 2005 1:16 pm
by Chris Corbyn
Not sure on how to use try/catch in JS but on a side-note: JavaScript is not Java... the two are unrelated and entirely separate. If you have been googling for try/catch in java you wont find javascript realted stuff ;)

Posted: Tue Oct 25, 2005 4:34 am
by Weirdan
http://developer.mozilla.org/en/docs/Co ... atch_Block

Code: Select all

// single catch block:
try {
  // statements
} catch(e) {
  // handle exceptions
}

// multiple catch blocks:
try {
  // statements
} catch(e if e == "ENoFile") {
  // handle ENoFile exceptions
} catch(e) {
  // handle other exceptions
}

Posted: Tue Oct 25, 2005 12:56 pm
by J_Iceman05
Thank you so much. I guess my main problem was just that i had "..." instead of a true variable name.
after i changed it to be "e" it seems to be working just fine. Thank you all for your help.
And thanks to those who at least looked at it with intent to solve my problem.