aboutsummaryrefslogtreecommitdiff
path: root/files/vi/web/javascript/reference/statements/throw
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:43:23 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:43:23 -0500
commit218934fa2ed1c702a6d3923d2aa2cc6b43c48684 (patch)
treea9ef8ac1e1b8fe4207b6d64d3841bfb8990b6fd0 /files/vi/web/javascript/reference/statements/throw
parent074785cea106179cb3305637055ab0a009ca74f2 (diff)
downloadtranslated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.tar.gz
translated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.tar.bz2
translated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.zip
initial commit
Diffstat (limited to 'files/vi/web/javascript/reference/statements/throw')
-rw-r--r--files/vi/web/javascript/reference/statements/throw/index.html193
1 files changed, 193 insertions, 0 deletions
diff --git a/files/vi/web/javascript/reference/statements/throw/index.html b/files/vi/web/javascript/reference/statements/throw/index.html
new file mode 100644
index 0000000000..c3116c8847
--- /dev/null
+++ b/files/vi/web/javascript/reference/statements/throw/index.html
@@ -0,0 +1,193 @@
+---
+title: throw
+slug: Web/JavaScript/Reference/Statements/throw
+translation_of: Web/JavaScript/Reference/Statements/throw
+---
+<div>{{jsSidebar("Statements")}}</div>
+
+<p>Câu lệnh <strong><code>throw</code> </strong> sẽ đưa ra một exception theo cách chúng ta định nghĩa. Các câu lệnh phía sau <code>throw</code> sẽ không được chạy, và sẽ gọi hàm callback <a href="/en-US/docs/Web/JavaScript/Reference/Statements/try...catch"><code>catch</code></a> đầu tiên tìm thấy. Nếu không có hàm <code>catch</code>, chương trình sẽ không chạy nữa.</p>
+
+<div>{{EmbedInteractiveExample("pages/js/statement-throw.html")}}</div>
+
+<p class="hidden">Source này được lưu trên GitHub repository. Nếu muốn đóng góp cho ví dụ này, bạn clone <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> và gởi lên pull request.</p>
+
+<h2 id="Cú_pháp">Cú pháp</h2>
+
+<pre class="syntaxbox">throw <em>expression</em>; </pre>
+
+<dl>
+ <dt><code>expression</code></dt>
+ <dd>Một diễn giải.</dd>
+</dl>
+
+<h2 id="Giải_thích">Giải thích</h2>
+
+<p>Sử dụng câu lệnh <code>throw</code> để đưa ra một exception. Giá trị của expression trả về có thể string, number, boolean, hay Object. Mỗi câu <code>throw </code>chỉ trả về một exception</p>
+
+<pre class="brush: js">throw 'Error2'; // 1 exception dạng string
+throw 42; // 1 exception giá trị 42
+throw true; // 1 exception với giá trị boolean là true
+throw new Error('Required'); // tạo một error object với nội dung Required
+</pre>
+
+<p>Câu lệnh <code>throw</code> tuân thủ nguyên tắc <a href="/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Automatic_semicolon_insertion">automatic semicolon insertion (ASI)</a> ,  nghĩa là không được phép xuống dòng giữa từ khóa <code>throw</code> và <code>expression</code>.</p>
+
+<h2 id="Ví_dụ">Ví dụ</h2>
+
+<h3 id="Throw_một_object">Throw một object</h3>
+
+<p>Exception có thể là một object. Lúc này có thể tham chiếu đến các property của object bên trong khối lệnh <code>catch</code> . Ví dụ sau, tạo một object với kiểu là <code>UserException</code> và sử dụng nó trong câu <code>throw</code>.</p>
+
+<pre class="brush: js">function UserException(message) {
+ this.message = message;
+ this.name = 'UserException';
+}
+function getMonthName(mo) {
+ mo = mo - 1; // Thay đổi giá trị của index array tương ứng cho tháng (1 = Jan, 12 = Dec)
+ var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul',
+ 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
+ if (months[mo] !== undefined) {
+ return months[mo];
+ } else {
+ throw new UserException('InvalidMonthNo');
+ }
+}
+
+try {
+ // statements to try
+ var myMonth = 15; // 15 nằm ngoài giá trị cho phép
+ var monthName = getMonthName(myMonth);
+} catch (e) {
+ monthName = 'unknown';
+ console.log(e.message, e.name); // truyền exception object vào câu lệnh xử lý nếu có lỗi
+}
+</pre>
+
+<h3 id="Một_ví_dụ_khác_sử_dụng_object">Một ví dụ khác sử dụng  object</h3>
+
+<p>Trong ví dụ sau, kiểm tra input, chỉ cho phép là giá trị U.S. zip code. Nếu giá trị zip code này không đúng format, throw một exception object là <code>ZipCodeFormatException</code>.</p>
+
+<pre class="brush: js">/*
+ * Creates a ZipCode object.
+ *
+ * Accepted formats for a zip code are:
+ * 12345
+ * 12345-6789
+ * 123456789
+ * 12345 6789
+ *
+ * If the argument passed to the ZipCode constructor does not
+ * conform to one of these patterns, an exception is thrown.
+ */
+
+function ZipCode(zip) {
+ zip = new String(zip);
+ pattern = /[0-9]{5}([- ]?[0-9]{4})?/;
+ if (pattern.test(zip)) {
+ // giá trị zip code value sẽ là giá trị đầu tiên khớp trong string
+ this.value = zip.match(pattern)[0];
+ this.valueOf = function() {
+ return this.value
+ };
+ this.toString = function() {
+ return String(this.value)
+ };
+ } else {
+ throw new ZipCodeFormatException(zip);
+ }
+}
+
+function ZipCodeFormatException(value) {
+ this.value = value;
+ this.message = 'does not conform to the expected format for a zip code';
+ this.toString = function() {
+ return this.value + this.message;
+ };
+}
+
+/*
+ * Đoạn script validate address theo kiểu US addresses.
+ */
+
+const ZIPCODE_INVALID = -1;
+const ZIPCODE_UNKNOWN_ERROR = -2;
+
+function verifyZipCode(z) {
+ try {
+ z = new ZipCode(z);
+ } catch (e) {
+ if (e instanceof ZipCodeFormatException) {
+ return ZIPCODE_INVALID;
+ } else {
+ return ZIPCODE_UNKNOWN_ERROR;
+ }
+ }
+ return z;
+}
+
+a = verifyZipCode(95060); // returns 95060
+b = verifyZipCode(9560); // returns -1
+c = verifyZipCode('a'); // returns -1
+d = verifyZipCode('95060'); // returns 95060
+e = verifyZipCode('95060 1234'); // returns 95060 1234
+</pre>
+
+<h3 id="Rethrow_một_exception">Rethrow một exception</h3>
+
+<p>Chúng ta có thể sử dụng <code>throw</code> để rethrow một exception sau khi đã catch nó. Trong ví dụ sau, catch lại exception nếu là giá trị lớn hơn 50 thì rethrow. Exception này sẽ được đưa lên hàm trên một cấp hoặc lên trên cùng cho các hàm catch khác.</p>
+
+<pre class="brush: js">try {
+ throw n; // throws một exception với giá trị là số
+} catch (e) {
+ if (e &lt;= 50) {
+ // câu lệnh xử lý cho exception từ 1-50
+ } else {
+ // không có xử lý cho trường hợp exception này, rethrow
+ throw e;
+ }
+}
+</pre>
+
+<h2 id="Specification">Specification</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">Specification</th>
+ <th scope="col">Status</th>
+ <th scope="col">Comment</th>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES3')}}</td>
+ <td>{{Spec2('ES3')}}</td>
+ <td>Khởi tạo. Hiện thực trong JavaScript 1.4</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES5.1', '#sec-12.13', 'throw statement')}}</td>
+ <td>{{Spec2('ES5.1')}}</td>
+ <td> </td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES6', '#sec-throw-statement', 'throw statement')}}</td>
+ <td>{{Spec2('ES6')}}</td>
+ <td> </td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ESDraft', '#sec-throw-statement', 'throw statement')}}</td>
+ <td>{{Spec2('ESDraft')}}</td>
+ <td> </td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Trình_duyệt_hổ_trợ">Trình duyệt hổ trợ</h2>
+
+
+
+<p>{{Compat("javascript.statements.throw")}}</p>
+
+<h2 id="Xem_thêm">Xem thêm</h2>
+
+<ul>
+ <li><a href="/en-US/docs/Web/JavaScript/Reference/Statements/try...catch"><code>try...catch</code></a></li>
+</ul>