Java có ba loại ngoại lệ là Error, Checked Exception, và Unckecked Exception. Error là loại ngoại lệ đặc biệt, lập trình viên không kiểm soát được, vì vậy ta sẽ tìm hiểu cách tùy biến cho hai loại ngoại lệ còn lại.
1. Tùy biến Checked Exception
Checked exception là exception bắt buộc lập trình viên phải handle nó khi bằng cách bao max code trong cặp try-cach. Một số checked exception phổ biến như IOException
, FileNotFoundException
(chúng đều được kế thừa từ lớp java.lang.Exception
).
Tạo lớp Checked Exception của riêng mình
Ta chỉ việc tạo một lớp exption và cho nó kế thừa lớp java.lang.Exception
.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<span class="hljs-keyword">package</span> com.viblo.exceptions; <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">NameNotFoundException</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Exception</span></span>{ <span class="hljs-comment">/** * */</span> <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">long</span> serialVersionUID = -<span class="hljs-number">3954969488995898575L</span>; <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">NameNotFoundException</span><span class="hljs-params">(String message)</span> </span>{ <span class="hljs-keyword">super</span>(message); } } |
Xửa dụng exception mới tạo ở trên
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<span class="hljs-keyword">package</span> com.viblo.examples; <span class="hljs-keyword">import</span> com.java.viblo.exceptions.NameNotFoundException; <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">CustomerService</span> </span>{ <span class="hljs-function"><span class="hljs-keyword">public</span> Customer <span class="hljs-title">findByName</span><span class="hljs-params">(String name)</span> <span class="hljs-keyword">throws</span> NameNotFoundException </span>{ <span class="hljs-keyword">if</span> (name == <span class="hljs-keyword">null</span> || name == <span class="hljs-string">""</span>) { <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> NameNotFoundException(<span class="hljs-string">"Name is NULL or EMPTY!"</span>); } <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> Customer(name); } <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{ CustomerService obj = <span class="hljs-keyword">new</span> CustomerService(); <span class="hljs-keyword">try</span> { Customer cus = obj.findByName(<span class="hljs-string">""</span>); } <span class="hljs-keyword">catch</span> (NameNotFoundException e) { e.printStackTrace(); } } } |
Exception throw
1 2 3 4 |
com<span class="hljs-selector-class">.viblo</span><span class="hljs-selector-class">.exceptions</span><span class="hljs-selector-class">.NameNotFoundException</span>: Name is NULL or EMPTY! at com<span class="hljs-selector-class">.viblo</span><span class="hljs-selector-class">.examples</span><span class="hljs-selector-class">.CustomerService</span><span class="hljs-selector-class">.findByName</span>(CustomerService<span class="hljs-selector-class">.java</span>:<span class="hljs-number">9</span>) at com<span class="hljs-selector-class">.viblo</span><span class="hljs-selector-class">.examples</span><span class="hljs-selector-class">.CustomerService</span><span class="hljs-selector-class">.main</span>(CustomerService<span class="hljs-selector-class">.java</span>:<span class="hljs-number">22</span>) |
2. Tùy biến Unchecked Exception
Unchecked exception hay còn gọi là Runtime exception. Là exception xảy ra khi chương trình đang chạy. Một số unchecked exception phổ biến như NullPointerException
, IndexOutOfBoundsException
, IllegalArgumentException
(chúng được kế thừa từ lớp java.lang.RuntimeException
).
Tạo một Unchecked exception
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<span class="hljs-keyword">package</span> com.viblo.exceptions; <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ListTooLargeException</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">RuntimeException</span></span>{ <span class="hljs-comment">/** * */</span> <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">long</span> serialVersionUID = -<span class="hljs-number">2931564504098722867L</span>; <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">ListTooLargeException</span><span class="hljs-params">(String message)</span> </span>{ <span class="hljs-keyword">super</span>(message); } } |
Xử dụng lớp exception trên
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
<span class="hljs-keyword">package</span> com.viblo.examples; <span class="hljs-keyword">import</span> java.util.ArrayList; <span class="hljs-keyword">import</span> java.util.Collections; <span class="hljs-keyword">import</span> java.util.List; <span class="hljs-keyword">import</span> com.viblo.exceptions.NameNotFoundException; <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">CustomerService</span> </span>{ <span class="hljs-function"><span class="hljs-keyword">public</span> Customer <span class="hljs-title">findByName</span><span class="hljs-params">(String name)</span> <span class="hljs-keyword">throws</span> NameNotFoundException </span>{ <span class="hljs-keyword">if</span> (name == <span class="hljs-keyword">null</span> || name == <span class="hljs-string">""</span>) { <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> NameNotFoundException(<span class="hljs-string">"Name is NULL or EMPTY!"</span>); } <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> Customer(name); } <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">analyze</span><span class="hljs-params">(List<String> data)</span> </span>{ <span class="hljs-keyword">if</span> (data.size() > <span class="hljs-number">50</span>) { <span class="hljs-comment">//runtime exception</span> <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> ListTooLargeException(<span class="hljs-string">"List can't exceed 50 items!"</span>); } <span class="hljs-comment">//...</span> } <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{ <span class="hljs-comment">// CustomerService obj = new CustomerService();</span> <span class="hljs-comment">//</span> <span class="hljs-comment">// try {</span> <span class="hljs-comment">//</span> <span class="hljs-comment">// Customer cus = obj.findByName("");</span> <span class="hljs-comment">//</span> <span class="hljs-comment">// } catch (NameNotFoundException e) {</span> <span class="hljs-comment">// e.printStackTrace();</span> <span class="hljs-comment">// }</span> CustomerService obj = <span class="hljs-keyword">new</span> CustomerService(); <span class="hljs-comment">//create 100 size</span> List<String> data = <span class="hljs-keyword">new</span> ArrayList<>(Collections.nCopies(<span class="hljs-number">100</span>, <span class="hljs-string">"Vunv"</span>)); obj.analyze(data); } } |
Exception throw
1 2 3 4 |
<span class="hljs-keyword">Exception</span> <span class="hljs-keyword">in</span> thread <span class="hljs-string">"main"</span> com.viblo.examples.ListTooLargeException: List can<span class="hljs-symbol">'t</span> exceed <span class="hljs-number">50</span> items! <span class="hljs-keyword">at</span> com.viblo.examples.CustomerService.analyze(CustomerService.java:<span class="hljs-number">24</span>) <span class="hljs-keyword">at</span> com.viblo.examples.CustomerService.main(CustomerService.java:<span class="hljs-number">47</span>) |
Trên đây là cách chúng ta tùy biến để tạo ra những exception (checked và unchecked) trong Java theo mục đích nghiệp vụ phát triển ứng dụng. Hi vọng các bạn cảm thấy relax với exception trong Java.