Fleek IT Solutions
Throws vs Throwable in Java Explained (70 อ่าน)
20 ส.ค. 2568 13:35
<p data-start="292" data-end="327"><span dir="auto" style="vertical-align: inherit;"><span dir="auto" style="vertical-align: inherit;">Hey everyone,</span></span>
<p data-start="329" data-end="625"><span dir="auto" style="vertical-align: inherit;"><span dir="auto" style="vertical-align: inherit;">I often see confusion around Java exception handling, especially when it comes to the difference between throws and Throwable. As part of a leading software testing company, we regularly deal with error handling and exception testing in different projects, so I thought I'd share some insights.</span></span>
<ul data-start="627" data-end="934">
<li data-start="627" data-end="814">
<p data-start="629" data-end="814"><span dir="auto" style="vertical-align: inherit;"><span dir="auto" style="vertical-align: inherit;">throws → Used in method declarations to specify which exceptions might be thrown. It's basically a way to alert the compiler and developers that a method can cause certain exceptions.</span></span>
</li>
<li data-start="815" data-end="934">
<p data-start="817" data-end="934"><span dir="auto" style="vertical-align: inherit;"><span dir="auto" style="vertical-align: inherit;">Throwable → This is the superclass of all exceptions and errors in Java. Both Exception and Error extend Throwable.</span></span>
</li>
</ul>
<p data-start="936" data-end="947"><span dir="auto" style="vertical-align: inherit;"><span dir="auto" style="vertical-align: inherit;">In short:</span></span>
<ul data-start="948" data-end="1058">
<li data-start="948" data-end="996">
<p data-start="950" data-end="996"><span dir="auto" style="vertical-align: inherit;"><span dir="auto" style="vertical-align: inherit;">throws = a keyword that declares exceptions.</span></span>
</li>
<li data-start="997" data-end="1058">
<p data-start="999" data-end="1058"><span dir="auto" style="vertical-align: inherit;"><span dir="auto" style="vertical-align: inherit;">Throwable = the root class for all exceptions and errors.</span></span>
</li>
</ul>
<p data-start="1060" data-end="1187"><span dir="auto" style="vertical-align: inherit;"><span dir="auto" style="vertical-align: inherit;">This distinction is crucial for writing stable, bug-free applications and for creating effective software testing strategies.</span></span>
<p data-start="1189" data-end="1419"><span dir="auto" style="vertical-align: inherit;"><span dir="auto" style="vertical-align: inherit;">We recently published a detailed article on this topic with code examples and practical explanations. If you want to dive deeper, you can check it out here: </span></span><br data-start="1345" data-end="1348" /> <span dir="auto" style="vertical-align: inherit;"><span dir="auto" style="vertical-align: inherit;">https://fleekitsolutions.com/difference-between-throws-and-throwable/</span></span>
<p data-start="1421" data-end="1543"><span dir="auto" style="vertical-align: inherit;"><span dir="auto" style="vertical-align: inherit;">Would love to hear your thoughts — how do you usually explain this concept to junior developers or testers on your team?</span></span>
<p data-start="1545" data-end="1594"><span dir="auto" style="vertical-align: inherit;"><span dir="auto" style="vertical-align: inherit;">— Fleek IT Solutions (Software Testing Company)</span></span>
Fleek IT Solutions
ผู้เยี่ยมชม