How To Deal With Different In Java : Guides For Selenium WebDriver
Click here: http://seleniuminterviewquestions.com/selenium-interview-questions-with-answers/
Anyone don’t like conditions and at the same time, anybody may not hide himself from exceptions when working webdriver software automation tests because (What’s An Exception? :-“>>) exception Is one event created during the performance of webdriver test case or java s/W program which disrupts evaluation delivery In between.
Exclusion can appear on account of the several reasons like, community connection or hardware failure, Unacceptable data entered by person, DB server down etc..
Therefore all these points may occurs any time when you operate your selenium webdriver test case and we can not stop It. Therefore Instead of thinking about stopping exclusion(Which is just not possible) during runtime, Believe about managing exceptions. Java software development language provides excellent exception handling mechanism to get over this sort of malfunctions. Let’s discover different ways of managing exclusion In java.
There are two sorts of exceptions In java as bellow.
1. Examined Exclusion :
Checked exceptions are these exceptions which are checked during compile time and needs catch block to found that exception during collection. If compiler will not locate catch block then it’s going to toss compilation error
. Really straightforward illustration of checked exclusion Is using Thread.sleep(5000); statement In your java application program code. If you will not place this assertion Inside attempt catch block subsequently it won’t let you gather your java software application.
2. Uncontrolled Exclusions :
Unrestrained different are these exception which are not assessed during compile-time. Normally checked exclusions happened because of the malfunction In code during run time. Most straightforward example of unchecked different Is int i = 4/0;. This statement may tosses / by zero different all through run time of java applications program.
Managing exclusions using try-catch block
We have to place try catch block around that code which might generate exclusion. In bellow provided example, System.out.println(a); Is composed Intentionally to create an different. If you see, that declaration Is composed Inside attempt block so If that declaration throw an exception – catch block may caught and handle It.
Here is the link: https://github.com/StackExchange/unikong/blob/gh-pages/assets/key.png
If you will operate above example In your eclipse, attempt prevent may be executed. “Before Exception” statement will be imprinted In console and Then assertion will generate an exception therefore “After Exception” declaration will be maybe not printed In games console and get a grip on will goes to catch prevent to handle that exception.
Consistently use try catch block to log your exclusion In selenium webdriver reviews.
Handling exclusions utilizing throws key word
Another way of management exception Is using tosses key word with process as revealed In holler given instance. Supposing you might have a throwexc method which Is tossing some exception and this process Is called from another system catchexc. Today you needs to handle exception of throwexc approach In to catchexc procedure then you certainly have to use throws keyword with throwexc approach.
Using toss Keyword To Toss An Exception Explicitly
Huge difference between throw and throws In java :
As we learned, punches keyword Is beneficial to throw these exceptions to contacting procedures that are not handled Inside named processes. Throw keyword h AS an alternative perform – throw keyword Is useful to throw an exception expressly whenever required. This Can Be the huge difference between throw and tosses In java. Interviewer can ask this question. Why don’t we appear at sensible example.
In preceding example, ArrayIndexOutOfBoundsException exception will be tossed by throw key word of throwexc approach.
Ultimately key word and Its use
Ultimately key word Is used with try catch prevent at the end of try catch block. Code written Inside finally prevent will be executed constantly regardless of exception Is happened or perhaps not. Principal intent of using finally with try catch prevent Is : In The Event you really wants to perform some action without contemplating exclusion occurred or maybe not. finally block will be do In both cases. Let’s see straightforward example of ultimately.
In above instance, 2 try-catch-finally obstructs employed. Run above example and discover consequence.
1st try prevent may throw an error and capture will handle and then finally prevent will be executed. 2nd try block is not going to throw any error so capture may be not executed but finally prevent may be executed. Therefore In both the cases finally prevent may be executed.