June 5, 2023
Try-catch
- try catch - yozgan kodimizda qandaydir xato bo'lsa anashu xatolikni tutib oladigan block, agar kodimizda xatolik bo'lsa projectni to'xtatib qo'yadi, try-catch orqali shu chiqqan xatoni userga custom xolatda ko'rinadigan qilsak bo'ladi.
try{
console.log('try block')
}catch(error){
console.log('error has occured', error.stack)
} finally{
console.log('this always runs')
}1-console try blockddagi kodda xatolik bo'lmasa ishlaydi
2-console try blockdagi kodda xatolik bo'lsa shu xatolikni catch bilan tutib olib uni consolega chiqaramiz.
3-console try yoki catch blok ishga tushganda chiqadi, ya'ni har doim.
- error.name - errorni qanaqa turdaligini bildiradi, // ReferenceError
- error.message - qanday errorligini bildiradi, // is not defined
- error.stack - errorni umumiy ko'rinishi, // ReferenceError: * is not defined
try{
console.log(a)
}catch(error){
console.log('error has occured', error.stack)
} finally{
console.log('this always runs')
} // a e'lon qilinmaganligi uchun xatolik chiqadi, catch dagi ma'lumotlarlet data = `{"age":30}`;
try {
let user = JSON.parse(data);
if (!user.name) {
throw new SyntaxError("Incomplete data: no name");
}
console.log(user.name);
} catch (error) {
console.log("JSON error:", error.name);
} // data ni ichida name yoqligi uchun xato chiqadi va throw orqali
// xatoni ozimizga custom xolatda chiqaryapmiz//ReferenceError
let x = 5;
try {
x = y + 1;
} catch (error) {
console.log(error instanceof ReferenceError);
console.log(error.name);
console.log(error.message);
}//SyntaxError
let a=5;
let a=10; // a already declared
==============
try {
eval("console.log('hello)");
} catch (error) {
console.log(error instanceof SyntaxError);\
console.log(error.name);
console.log(error.message);
} // invalid or unexpected token (')//Typeerror
const a=5;
a=7; // const is not changeable
==========
let num = 5;
try {
console.log(num.toUppercase());
} catch (error) {
console.log(error.name);
} // number has not toUpperCase method//RangeError
let num = 5;
try {
console.log(num.toPrecision(500));
} catch (error) {
console.log(error.name);
console.log(error.message)
} - promise - asinxron amallarni bajarish uchun ishlatiladi, ya'ni qanday amalni kutib turadi va keyin natijani yoki xatolikni chiqaradi.
- promise o'ziga 2 ta parametr oladi (resovle va reject), resovle- hich qanday muammo bo'lmaganda ishlaydi, reject esa kodda qandaydir muammo bo'lsa ishlaydi.
//Syntax
new Promise((resolve, reject)=>{
let error = false;
if(!error){
resovle()
} else{
reject()
}
})
//Example
let promise = new Promise((resolve, reject) => {
let num = 3;
if (num == 3) {
resolve("Test passed");
} else {
reject("Test didn't pass");
}
});
promise
.then((message) => {
console.log("This is in the then block", message);
})
.catch((message) => {
console.log("This is in the catch block", message);
});async, await - xatolik bo'lsa try catch bilan tutib olishimiz mumkin bo'ladi
//Syntax
async function getData(){
try{
await render();
} catch(error){
console.log(error.message)
}
}