JavaScript try...catch | Error Handling | try catch finally #kody_az

Dərs haqqında

JavaScript-də try...catch ifadəsi proqramın icrası zamanı baş verə biləcək səhvləri idarə etmək üçün güclü bir mexanizmdir. Bu mexanizm səhvlərin proqramı dayandırmadan həll olunmasına imkan verir.

1. try Bloku:
Bu blokda səhv baş verə biləcək kod yerləşdirilir. JavaScript mühərriki try blokundakı kodu icra etməyə çalışır. Əgər bu blokda səhv baş verərsə, idarə dərhal catch blokuna keçir.

2. catch Bloku:
Əgər try blokunda səhv baş verərsə, catch bloku icra olunur. Bu blok səhvi idarə edir. Buradakı səhv obyekti (məsələn, error) baş verən səhv haqqında məlumatı özündə saxlayır, məsələn, səhv növü, mesajı və bəzən də səhv meydana gələn kod yeri.

3. finally Bloku (İstəyə bağlı):
finally bloku istəyə bağlıdır. Əgər onu əlavə etsəniz, içərisindəki kod səhv olub-olmamasından asılı olmayaraq hər zaman icra olunacaq. Bu blok, faylların bağlanması və ya resursların azad edilməsi kimi təmizləmə işləri üçün faydalıdır.

ECMAScript 2019 (ES10) ilə, əgər siz səhv obyektinə ehtiyac duymursunuzsa, catch blokunda səhv parametrini buraxa bilərsiniz. Bu, səhv məlumatlarını əldə etmədən sadəcə ümumi idarəetmə aparmaq istədiyiniz hallarda faydalıdır.

Səhv Tipləri:

1) SyntaxError: Sintaksis səhvi baş verdikdə (bu səhvi runtime zamanı tutmaq olmur).
2) TypeError: Gözlənilən tipdə olmayan bir dəyər istifadə edildikdə.
3) ReferenceError: Mövcud olmayan dəyişənə müraciət edildikdə.
4) RangeError: Rəqəmsal dəyişənin və ya parametrin icazə verilən aralıqdan kənara çıxdığı halda.
5) EvalError: eval() funksiyası ilə bağlı problem baş verdikdə (çox nadir hallarda).
6) URIError: URI qlobal funksiyalarında (encodeURI, decodeURI) problem olduqda.

=======

The try...catch statement in JavaScript is a powerful mechanism for handling runtime errors in your code without stopping the execution of the program.

1. try Block:
This block contains the code that may potentially cause an error. The JavaScript engine will attempt to execute everything inside the try block. If an error occurs during the execution, control will immediately pass to the catch block.

2. catch Block:
The catch block is executed if an error occurs in the try block. This is where you handle the error. The error object passed to catch contains information about what went wrong, including the error message, error type, and sometimes the stack trace (where the error occurred in the code).

3. finally Block (Optional):
The finally block is optional. If you include it, the code inside it will always run, regardless of whether an error was thrown in the try block or not. This is useful for cleanup tasks, such as closing files or releasing resources.

As of ECMAScript 2019 (ES10), you can omit the error argument in the catch block if you don’t need to access the error object. This is useful when you don't care about the specific error details and just want to handle it generically.

JavaScript has several built-in error types that you may encounter:

1) SyntaxError: Occurs when there is a syntax mistake in the code (cannot be caught at runtime).
2) TypeError: Occurs when a value is not of the expected type.
3) ReferenceError: Occurs when trying to access a variable that doesn’t exist.
4) RangeError: Occurs when a numeric variable or parameter is outside its valid range.
5) EvalError: Occurs when there’s an issue with the eval() function (rarely used).
6) URIError: Occurs when there’s an issue with global URI handling functions (encodeURI, decodeURI).

#kody_az #kodyaz #js #javascript #nodejs #es6 #viral #viralcode #viralvideo #random #code #coder #azerbaycan #azerbaijan #baku #baki #programming #programmer #codes #hazırlıq #hazirliq #kurs #kurslar #course #courses #online #onlinecourses #test #quiz

YouTube-da bax · KODY.AZ