throw keyword:
- throw keyword used to throw user defined exceptions.(we can throw predefined exception too)
- If we are having our own validations in our code we can use this throw keyword.
- For Ex: BookNotFoundException, InvalidAgeException (user defined).
Program:
- package com.instanceofjava;
- public class MyExceptionThrow {
- public static void main(String a[]){
- try{
- MyExceptionThrow thr = new MyExceptionThrow();
- System.out.println("length of INDU is "+thr.getStringSize("INDU"));
- System.out.println("length of SAIDESH is "+thr.getStringSize("SAIDSH"));
- System.out.println("length of null string is "+thr.getStringSize(null));
- }
- catch (Exception ex){
- System.out.println("Exception message: "+ex.getMessage());
- }
- }
- public int getStringSize(String str) throws Exception{
- if(str == null){
- throw new Exception("String input is null");
- }
- return str.length();
- }
- }
Output
length of INDU is 4
length of SAIDESH is 5
Exception message: String input is null
throws keyword:
- The functionality of throws keyword is only to explicitly to mention that the method is proven transfer un handled exceptions to the calling place.
Program:
- package com.instanceofjava;
- public class ExcpetionDemo {
- public static void main(String agrs[]){
- try
- {
- //statements
- }
- catch(Exception e)
- {
- System.out.println(e);
- }
- finally(){compulsorily executable statements
- }
- }
- }
Thanks for sharing valuable information.Great post!It was very useful for me. Keep giving such type of nice information.😊😊😊
ReplyDeleteBest Selenium Training Institute In Hyderabad | Online Selenium Training
Good Post. I like your blog. Thanks for Sharing
ReplyDeleteSelenium Training in Gurgaon
nice course. thanks for sharing this post this post harried me a lot.
ReplyDeleteSelenium Training in Gurgaon
Your all contents are copied from Sitesbay.com Within 3 days remove all these contents otherwise i will go with DMCA.
DeleteYour all contents are copied from Sitesbay.com Within 3 days remove all these contents otherwise i will go with DMCA.
ReplyDeleteYour all contents are copied from Sitesbay.com Within 3 days remove all these contents otherwise i will go with DMCA.
ReplyDeleteYour all contents are copied from Sitesbay.com Within 3 days remove all these contents otherwise i will go with DMCA.
ReplyDeleteBest article, very useful and explanation. Your post is extremely incredible. Thank you very much for the new information.
ReplyDeleteSelenium Training Institute in Pune
The article is so appealing. You should read this article before choosing the Big data engineering services you want to learn.
ReplyDeleteCore Java With Selenium blogspot simply awesome.
ReplyDeleteManual Testing Training in Chennai
QTP Training in Chennai
Selenium Training in Chennai
SoapUI Training in Chennai
Software Testing Training in Chennai
Thank you for sharing. very helpful Blog!!
ReplyDeleteDevOps Training
DevOps Online Training
This comment has been removed by the author.
ReplyDelete