Hacker News new | past | comments | ask | show | jobs | submit login

That won't work because if thing1() throws you usually don't want to process to thing2

Take this for example:

     const response = await fetch('./api.json');
        try {
            const json = await response.json();
            console.log('YAY! JSON!', json);
        } catch (jsonParseError) {
            alert('damn it!');
        }
     } catch(fetchError) {
        console.warn('this is not cool');
     }



Okay, that's a fair counter-argument. My gut feeling is that's possibly one catch to many and as much as possible I'd want to try to merge the two catches/unifying the error response logic.

Another idea is that you could reformat to a "parade" try/catch by adding returns to the catch:

    try {
        const response = await fetch('./api.json')
    } catch (fetchError) {
        console.warn('this is not cool')
        return // Exit
    }
    try {
        const json = await response.json();
        console.log('YAY! JSON!', json);
    } catch (jsonParseError) {
        alert('damn it!');
    }
Obviously you'd need to move any code you'd want to run regardless of error up into the parent function or out into a wrapper function.




Consider applying for YC's Summer 2025 batch! Applications are open till May 13

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: