The catch block is not executed in typescript

I have a simple channel that formats the passed parameters into a date format. If this conversion is incorrect, it causes an error. But it never throws an error in a catch block.

import {PipeTransform, Pipe} from 'angular2/core';

@Pipe({
    name: 'formatDate'
})

export class FormatDatePipe implements PipeTransform {
    transform(value: string): any {
        let date: string;
        try {
            date = new Date(value).toLocaleDateString();
        }
        catch (Exception) {
            return value;
        }
        finally {            
            return date;
        }        
    }

Why do catch locks fail even if an invalid date is passed?

+4
source share
1 answer

If you pass an invalid date to the constructor, then it does not throw an error for all inputs, it depends.

You can read about it here: Rollback to specific application date formats that reference this " rough outline on how parsing works .

, , Invalid Date, :

try {
    date = new Date(value).toLocaleDateString();
    if (date === "Invalid Date") {
        throw new Error(`invalid date value ${ value }`);
    }
}

.

+2

Source: https://habr.com/ru/post/1652661/


All Articles