Skip to content

Multi-catch support #30830

Closed
Closed
@brunoc107

Description

@brunoc107

Hello, everyone

I've been working with typescript quite a wile since i've first found it, and it's one of the coolest piece of technology i've ever used. But I think it could be cooler if we had a better way of handling errors. I mean, we could leverage the functionality of the language's try-catch.
Sometimes we need to handle some errors in specific ways that do not apply to others and the current language does not support a more strait forward way of doing it.
Today, we need to do something like this:

try {
	// Here we throw CustomError
	someOperation();
	// Here we throw AnotherCustomError
	anotherOperation();
} catch (e) {
	if (e instanceof CustomError) {
		// Custom handling
	} else if (e instanceof AnotherCustomError) {
		// Another custom handling
	} else {
		// Default error handling
	}
}

I think it could be better. We could add support to "multi-catch" to the language. The code that follows should be transpiled to the code seen before:

try {
	// Here we throw CustomError
	someOperation();
	// Here we throw AnotherCustomError
	anotherOperation();
} catch (e: CustomError) {
	// Custom handling
} catch (e: AnotherCustomError) {
	// Another custom handling
} catch (e) {
	// Default error handling
}

What do you think about it, guys?
I think it would be a joy to see it working!

Regards :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Out of ScopeThis idea sits outside of the TypeScript language design constraintsSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions