### Handling Illegal Date Errors in tyme4ts Source: https://github.com/6tail/tyme4ts/blob/master/_autodocs/errors.md Demonstrates how to catch and handle 'illegal solar day' errors when creating dates with invalid combinations or out-of-range values. It shows both direct try-catch and a recommended safe creation function. ```typescript import { SolarDay } from 'tyme4ts'; try { // 尝试创建2月30日 const day = SolarDay.fromYmd(2024, 2, 30); } catch (error) { if (error instanceof Error && error.message.includes('illegal solar day')) { console.error('日期无效:', error.message); // 处理错误,例如使用有效日期 } } // 推荐做法:先验证 function createSolarDaySafely(year: number, month: number, day: number): SolarDay | null { try { SolarDay.validate(year, month, day); return SolarDay.fromYmd(year, month, day); } catch (error) { console.error('日期验证失败'); return null; } } const day = createSolarDaySafely(2024, 2, 30); if (day === null) { // 处理无效日期 } ``` -------------------------------- ### Handling Illegal Number Parameter Errors Source: https://github.com/6tail/tyme4ts/blob/master/_autodocs/errors.md Shows how to catch and manage 'illegal ' errors that arise when string parameters cannot be converted to numbers or when parameters are NaN. Includes a safe parsing function. ```typescript import { SolarDay } from 'tyme4ts'; try { // 传入非数字字符串 const day = SolarDay.fromYmd('2024', 'invalid', 1); } catch (error) { console.error('参数类型错误:', error.message); } // 安全的参数转换 function parseDate(year: any, month: any, day: any): SolarDay | null { try { const y = Number(year); const m = Number(month); const d = Number(day); if (isNaN(y) || isNaN(m) || isNaN(d)) { throw new Error('参数必须是有效的数字'); } return SolarDay.fromYmd(y, m, d); } catch (error) { console.error('解析失败:', error); return null; } } ``` -------------------------------- ### Handling Invalid Cycle Index Errors Source: https://github.com/6tail/tyme4ts/blob/master/_autodocs/errors.md Shows how to catch errors for out-of-range indices for Heaven Stem (0-9), Earth Branch (0-11), and Sixty Cycle (0-59). Includes a safe function for retrieving Heaven Stem by index. ```typescript import { HeavenStem, EarthBranch, SixtyCycle } from 'tyme4ts'; try { // 无效的索引 const stem = HeavenStem.fromIndex(10); // 天干只有10个(0-9) } catch (error) { console.error('索引超出范围:', error.message); } // 安全地处理索引 function getHeavenStemSafely(index: number): HeavenStem | null { try { if (index < 0 || index > 9) { throw new Error(`天干索引必须在0-9之间,收到: ${index}`); } return HeavenStem.fromIndex(index); } catch (error) { console.error('获取天干失败:', error.message); return null; } } ``` -------------------------------- ### Handling Invalid Time Unit Range Errors Source: https://github.com/6tail/tyme4ts/blob/master/_autodocs/errors.md Demonstrates catching errors for invalid time ranges, such as hours outside 0-23, minutes outside 0-59, or seconds outside 0-59. Provides a safe time creation function. ```typescript import { SolarTime } from 'tyme4ts'; try { // 无效的时间:小时超出范围 const time = SolarTime.fromYmdHms(2024, 1, 1, 25, 0, 0); } catch (error) { console.error('时间无效:', error.message); } // 安全的时间创建 function createSolarTimeSafely( year: number, month: number, day: number, hour: number, minute: number, second: number ): SolarTime | null { try { SolarTime.validate(year, month, day, hour, minute, second); return SolarTime.fromYmdHms(year, month, day, hour, minute, second); } catch (error) { console.error('时间验证失败:', error.message); return null; } } ``` -------------------------------- ### Handling Gregorian Calendar Reform Date Error Source: https://github.com/6tail/tyme4ts/blob/master/_autodocs/errors.md Illustrates catching an 'illegal solar day' error that occurs when attempting to create a date between October 5th and 14th, 1582, due to the Gregorian calendar reform. ```typescript import { SolarDay } from 'tyme4ts'; try { // 这个日期不存在(格里历改革) const day = SolarDay.fromYmd(1582, 10, 10); } catch (error) { console.error(error.message); // 错误: illegal solar day: 1582-10-10 } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.