錯(cuò)誤,與提示有所不同,將會(huì)中斷編譯器的下一步進(jìn)程。基本上,它們中斷編譯并像堆棧跟蹤一樣在輸出流中顯示相關(guān)信息,這對(duì)調(diào)試很有幫助。因此,如果程序無(wú)法繼續(xù)執(zhí)行就應(yīng)該拋出錯(cuò)誤。如有可能,嘗試解決這個(gè)問(wèn)題并以顯示提醒的方式代替。
舉個(gè)例子,假設(shè)你創(chuàng)建了一個(gè) getter 函數(shù)來(lái)從特定 map 中獲取值。如果想要獲取的值并不在 map 中,就可能會(huì)拋出錯(cuò)誤。
/// Z-indexes map, gathering all Z layers of the application
/// @access private
/// @type Map
/// @prop {String} key - Layer’s name
/// @prop {Number} value - Z value mapped to the key
$z-indexes: (
'modal': 5000,
'dropdown': 4000,
'default': 1,
'below': -1,
);
/// Get a z-index value from a layer name
/// @access public
/// @param {String} $layer - Layer's name
/// @return {Number}
/// @require $z-indexes
@function z($layer) {
@if not map-has-key($z-indexes, $layer) {
@error 'There is no layer named `#{$layer}` in $z-indexes. '
+ 'Layer should be one of #{map-keys($z-indexes)}.';
}
@return map-get($z-indexes, $layer);
}
更多建議: