Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 909407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:52:22+00:00 2026-05-15T16:52:22+00:00

Would it be faster to just put code inside a try-catch block instead of

  • 0

Would it be faster to just put code inside a try-catch block instead of performing various error checks?

For example..

function getProjectTask(projectTaskId) {
    if (YAHOO.lang.isUndefined(projectTaskId) || YAHOO.lang.isNull(projectTaskId) && !YAHOO.lang.isNumber(projectTaskId)) {
        return null;
    }

    var projectPhaseId, projectPhaseIndex, projectTaskIndex, projectPhases, projectPhase, projectTask;

    if (!YAHOO.lang.hasOwnProperty(projectTaskPhaseMap, projectTaskId)) {
        return null;
    }

    projectPhaseId = projectTaskPhaseMap[projectTaskId];

    if (YAHOO.lang.isUndefined(projectPhaseId) || YAHOO.lang.isNull(projectPhaseId) || !YAHOO.lang.hasOwnProperty(scheduleData.ProjectPhasesMap, projectPhaseId)) {
        return null;
    }

    projectPhaseIndex = scheduleData.ProjectPhasesMap[projectPhaseId];
    if (YAHOO.lang.isUndefined(projectPhaseIndex) || YAHOO.lang.isNull(projectPhaseIndex) || !YAHOO.lang.hasOwnProperty(scheduleData.ProjectPhases[projectPhaseIndex])) {
        return null;
    }
    projectPhase = scheduleData.ProjectPhases[projectPhaseIndex];

    if (!YAHOO.lang.hasOwnProperty(projectPhase.ProjectTasksMap, projectTaskId)) {
        return null;
    }

    projectTaskIndex = projectPhase.ProjectTasksMap[projectTaskId];

    if (YAHOO.lang.isUndefined(projectTaskIndex) || YAHOO.lang.isNull(projectTaskIndex)) {
        return null;
    }

    projectTask = scheduleData.ProjectTasks[projectTaskIndex];
}

VS

function getProjectTask(projectTaskId) {
    try {
        projectPhaseId = projectTaskPhaseMap[projectTaskId];
        projectPhaseIndex = scheduleData.ProjectPhasesMap[projectPhaseId];
        projectPhase = scheduleData.ProjectPhases[projectPhaseIndex];
        projectTaskIndex = projectPhase.ProjectTasksMap[projectTaskId];
        projectTask = scheduleData.ProjectTasks[projectTaskIndex];

    }
    catch (e) {
        return null;
    }
}

I hope my question makes sense. I would be happy to clarify.
Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T16:52:23+00:00Added an answer on May 15, 2026 at 4:52 pm

    "Programs must be written for people
    to read, and only incidentally for
    machines to execute."

    Abelson & Sussman, SICP, preface to the first edition

    Always aim for readable code. The key thing to remember is:

    Avoid try-catch in performance-critical functions, and loops

    Anywhere else they won’t do much harm. Use them wisely, use them when they make sense.

    But as I see you clearly misuse some functions for error checking. You can test for the desired objects and properties of objects right before you use them instead of complex checking. And:

    if (YAHOO.lang.isUndefined(projectPhaseId) || YAHOO.lang.isNull(projectPhaseId))
    

    can be written as

    if (projectPhaseId != null)
    

    for example… So the example above can be fairly readable even without try catches. You seem to misuse YUI a bit.

    I would bet this works as expected:

    function getProjectTask(projectTaskId) {
    
       var projectPhaseId    = projectTaskPhaseMap[projectTaskId],
           projectPhaseIndex = scheduleData.ProjectPhasesMap[projectPhaseId],
           projectPhase      = scheduleData.ProjectPhases[projectPhaseIndex];
    
      if (projectPhase == null) return null; // projectPhase would break the chain
    
      var projectTaskIndex  = projectPhase.ProjectTasksMap[projectTaskId],
          projectTask       = scheduleData.ProjectTasks[projectTaskIndex];
    
       return projectTask || null; // end of the dependency chain
    
    }
    

    How cool is that? 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 498k
  • Answers 498k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer http://spuntinu.net/insert-a-delay-on-large-drop-down-menu-with-prototype/ used to do the trick. Now, see http://backfromcamden.net/insert-a-delay-on-large-drop-down-menu-with-prototype/ [Thanks,… May 16, 2026 at 12:19 pm
  • Editorial Team
    Editorial Team added an answer It does not make sense to have a Product or… May 16, 2026 at 12:19 pm
  • Editorial Team
    Editorial Team added an answer Yes. Use if IsWin64 then // Do sth else //… May 16, 2026 at 12:19 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

My primary question is which approach is faster. Some briefing I'm developing an application
I'm using a GZIPInputStream in my program, and I know that the performance would
While the question check if input is type of string has been closed two
I'm wonder how people are kicking people out or blocking access to a site
I'm working on a community based website using zend framework - but its so
I'm reading a custom table cell in tableView:cellForRowAtIndexPath: from a nib file. This works
Data: $data = array('Alice', 'Bob', 'Carol', 'David', 'Elizabeth', 'Frank'); Method A: file_put_contents('filename.ext', implode(\n, $data)
I'm wondering if there are any 2D graph plotting libraries that run on the
So earlier I answered my own question on thinking in vectors in R. But
I'm drawing 2D, concave, sometimes multicontoured, sometimes self intersecting polygons with OpenGL. Here is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.