except-js provides the necessary abstractions for handling exceptions of different types within multiple catch statements. This is pretty standard on strongly typed languages like Java.
const { create, except } = require('except-js')
const Exception1Test = create('Exception1Test')
const Exception2Test = create('Exception2Test')
function helloWorld(){
throw new Exception1Test('ExceptionTest here!')
}
try {
helloWorld()
}catch(exception){
except(exception)
.on(Exception1Test)
.do(() => {
console.log('[Exp1] Message:', exception.message)
})
.on(Exception2Test)
.do(() => {
console.log('[Exp2] Message:', exception.message)
})
.done()
}
const { create, handle } = require('except-js')
const ExceptionTest = create('ExceptionTest')
function helloWorld(){
throw new ExceptionTest('ExceptionTest here!')
}
try {
helloWorld()
}catch(exception){
handle(exception, [
{
exception: ExceptionTest,
handler: () => {
console.log('Message:', exception.message)
}
}
])
}
Note: If no matching handler is passed, the exception is thrown again. This is a design decision in order to preserve the decoupling of the sender from the receiver of the exception (exception propagation across the stack).
MIT