Core JAVA

User defined Exception in Java

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">You can also create your own exception sub class simply by extending java <i><b>Exception <&sol;b><&sol;i>class&period; You can define a constructor for your Exception sub class &lpar;not compulsory&rpar; and you can override the <b><i>toString&lpar;&rpar;<&sol;i><&sol;b> function to display your customized message on catch&period;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">class InvalidAgeException extends Exception&lbrace; &NewLine; InvalidAgeException&lpar;String s&rpar;&lbrace; &NewLine; super&lpar;s&rpar;&semi; &NewLine; &rcub; &NewLine; public String toString&lpar;&rpar; &NewLine; &lbrace; &NewLine; return "Candidate is less than 18 year is not allowed to vote&period;"&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<div align&equals;'center' id&equals;"ads-id"><&sol;div>&NewLine;<pre class&equals;"highlight" name&equals;"code">class ValidateCandidate&lbrace; &NewLine; &NewLine; static void validate&lpar;int age&rpar; throws InvalidAgeException&lbrace; &NewLine; if&lpar;age &lt&semi; 18&rpar; &NewLine; throw new InvalidAgeException&lpar;"invalid candidate"&rpar;&semi; &NewLine; else &NewLine; System&period;out&period;println&lpar;"welcome to vote"&rpar;&semi; &NewLine; &rcub; &NewLine; &NewLine; public static void main&lpar;String args&lbrack;&rsqb;&rpar;&lbrace; &NewLine; try&lbrace; &NewLine; validate&lpar;13&rpar;&semi; &NewLine; &rcub;catch&lpar;Exception ex&rpar;&lbrace; &NewLine; System&period;out&period;println&lpar;"Exception occured&colon; "&plus;ex&rpar;&semi; &NewLine; &rcub; &NewLine; &NewLine; System&period;out&period;println&lpar;"rest of the code&period;&period;&period;"&rpar;&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p>&NewLine;<b>output here&colon;<&sol;b><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;04&sol;userdefine&period;png"&sol;><&sol;div>&NewLine;<p><b>Points to Remember<&sol;b><&sol;p>&NewLine;<p><&sol;p>&NewLine;<ol>&NewLine;<li>Extend the Exception class to create your own <i><b>exception <&sol;b><&sol;i>class&period;<&sol;li>&NewLine;<li>You don&&num;8217&semi;t have to implement anything inside it&comma; no methods are required&period;<&sol;li>&NewLine;<li>You can have a Constructor if you want&period;<&sol;li>&NewLine;<li>You can override the <i><b>toString&lpar;&rpar;<&sol;b><&sol;i> function&comma; to display customized message&period;<&sol;li>&NewLine;<&sol;ol>&NewLine;<p><&sol;p>&NewLine;<div style&equals;"background-color&colon; pink&semi; border-width&colon; thin&semi; text-align&colon; center&semi;"><b>&lt&semi;&lt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;handle-exceptions-in-overriding-methods&sol;">Previous<&sol;a> &lt&semi;&lt&semi;&nbsp&semi;&nbsp&semi; &vert;&vert; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;core-java-baby-step-to-be-best-java-ian&sol;">Index <&sol;a>&vert;&vert; &nbsp&semi; &gt&semi;&gt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;multiple-exceptions-in-java-7-new&sol;">Next<&sol;a> &gt&semi;&gt&semi;<&sol;b><&sol;div>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;handle-exceptions-in-overriding-methods&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;different-exception-generate-in-array&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '495'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design PatternsĀ using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago