Javascript multiple “try”s

后端 未结 3 594
不思量自难忘°
不思量自难忘° 2021-01-14 04:53

We all know the basic escaping mechanism in JS:

try {
    ...
}
catch(err) {
    ..
}

I have a JSON data in which I want to check if a lead

3条回答
  •  余生分开走
    2021-01-14 05:11

    You could nest your second try/catch block in the exception block of the first try/catch block as follows:

    try {
        name = lead['Details']['Name']['Full'];
    } catch(ex0) {      
      try {
          name = lead['Details']['Name']['First'] + " " + lead['Details']['Name']['Last'];
      } catch (ex1) {
          name = "No Name";
      }
    }
    

    This is valid syntax, and functionally equivalent to what you're requiring

提交回复
热议问题