Šajā apmācībā jūs uzzināsit par JavaScript mest paziņojumiem ar piemēru palīdzību.
Iepriekšējā apmācībā jūs iemācījāties rīkoties ar izņēmumiem, izmantojot JavaScript try… catch note. Izmēģinājumu un nozvejas paziņojumi izņēmumus apstrādā standarta veidā, ko nodrošina JavaScript. Tomēr throw
paziņojumu varat izmantot, lai nodotu lietotāja definētus izņēmumus.
Programmā JavaScript throw
paziņojums apstrādā lietotāja definētus izņēmumus. Piemēram, ja noteikts skaitlis ir dalīts ar 0 un ja jums tas jāņem vērā Infinity
kā izņēmums, varat izmantot throw
priekšrakstu, lai rīkotos ar šo izņēmumu.
JavaScript mest paziņojums
Metiena paziņojuma sintakse ir šāda:
throw expression;
Šeit expression
norāda izņēmuma vērtību.
Piemēram,
const number = 5; throw number/0; // generate an exception when divided by 0
Piezīme : izteiksme var būt virkne, būla skaitlis, skaitlis vai objekta vērtība.
JavaScript mest ar mēģinājumu … noķert
Sintakse try… catch… throw
ir:
try ( // body of try throw exception; ) catch(error) ( // body of catch )
Piezīme : Kad izpildīts metiena paziņojums, tas iziet no bloka un dodas uz catch
bloku. Un kods zem throw
paziņojuma netiek izpildīts.
1. piemērs: mēģiniet … noķert … mest Piemērs
const number = 40; try ( if(number> 50) ( console.log('Success'); ) else ( // user-defined throw statement throw new Error('The number is low'); ) // if throw executes, the below code does not execute console.log('hello'); ) catch(error) ( console.log('An error caught'); console.log('Error message: ' + error); )
Rezultāts
Pieņemta kļūda Kļūdas ziņojums: Kļūda: skaits ir mazs
Iepriekš minētajā programmā tiek pārbaudīts nosacījums. Ja skaitlis ir mazāks par 51 , tiek izmesta kļūda. Un šī kļūda tiek izmesta, izmantojot throw
paziņojumu.
throw
Paziņojumā norāda virkni The number is low
kā izteiksmi.
Piezīme : Jūs varat izmantot arī citus iebūvētās kļūdas konstruktoru standarta kļūdas: TypeError
, SyntaxError
, ReferenceError
, EvalError
, InternalError
, un RangeError
.
Piemēram,
throw new ReferenceError('this is reference error');
Atkārtojiet izņēmumu
Jūs varat arī izmantot throw
paziņojumu catch
blokā, lai atkārtoti parādītu izņēmumu. Piemēram,
const number = 5; try ( // user-defined throw statement throw new Error('This is the throw'); ) catch(error) ( console.log('An error caught'); if( number + 8> 10) ( // statements to handle exceptions console.log('Error message: ' + error); console.log('Error resolved'); ) else ( // cannot handle the exception // rethrow the exception throw new Error('The value is low'); ) )
Rezultāts
Pieņemta kļūda Kļūdas ziņojums: Kļūda: Šī ir novērsta metiena kļūda
Iepriekš minētajā programmā throw
paziņojums tiek izmantots try
blokā, lai noķertu izņēmumu. Un throw
paziņojums tiek atkārtoti ierakstīts catch
blokā, kas tiek izpildīts, ja catch
bloks nevar apstrādāt izņēmumu.
Šeit catch
bloks apstrādā izņēmumu, un kļūda nenotiek. Tādējādi throw
paziņojums netiek atkārtoti apstiprināts.
Ja kļūdu neizturēja uztveršanas bloks, metiena paziņojums tiktu atkārtoti parādīts ar kļūdas ziņojumu Uncaught Error: vērtība ir zema