JavaScript mest paziņojums

Š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 throwpaziņojumu varat izmantot, lai nodotu lietotāja definētus izņēmumus.

Programmā JavaScript throwpaziņ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ā Infinitykā izņēmums, varat izmantot throwpriekšrakstu, lai rīkotos ar šo izņēmumu.

JavaScript mest paziņojums

Metiena paziņojuma sintakse ir šāda:

 throw expression;

Šeit expressionnorā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… throwir:

 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 catchbloku. Un kods zem throwpaziņ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 throwpaziņojumu.

throwPaziņojumā norāda virkni The number is lowkā 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 throwpaziņojumu catchblokā, 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ā throwpaziņojums tiek izmantots tryblokā, lai noķertu izņēmumu. Un throwpaziņojums tiek atkārtoti ierakstīts catchblokā, kas tiek izpildīts, ja catchbloks nevar apstrādāt izņēmumu.

Šeit catchbloks apstrādā izņēmumu, un kļūda nenotiek. Tādējādi throwpaziņ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

Interesanti raksti...