How To Deal With Different In Java : Lessons For Selenium WebDriver
Anyone don’t like conditions and at the same time, anybody may not conceal himself from exceptions when working webdriver application robotization evaluations because (What Is An Exclusion? :-“>>) exception Is an error occasion generated during the delivery of webdriver test case or java software package which disturbs test delivery In between.
Exception can arise due to the many reasons like, community connection or hardware failure, Broken data entered by person, DB host down etc..
So all these points can occurs any period when you operate your selenium webdriver test suit and we CAn’t stop It. So Instead of thinking about stopping exception(Which Is not potential) during runtime, Think about handling exceptions.
Java it development terminology provides very good exception handling mechanism to get over this sort of malfunctions. Let’s understand different ways of management exclusion In java.
There are two kinds of exclusions In java as bellow.
1. Checked Exception :
Checked exceptions are those exceptions which are checked during compile time and wants catch block to captured that exclusion during system
If compiler will not find catch prevent afterward It will toss compilation error. Really simple instance of examined exclusion Is using Thread.sleep(5000); statement In your java application program signal.
If you’ll not place this assertion Inside try catch prevent then it won’t allow you to gather your java s/W application.
Click here: http://seleniumwebdrivertutorial.com/courses/download-file-using-selenium/
2. Uncontrolled Conditions :
Unrestrained exception are those exception that aren’t checked during compile-time. Generally checked exceptions occurred because of the error In signal during run-time.
Most straightforward example of uncontrolled different Is int i = 4/0;. This statement may throws / by zero different all through run-time of java software application.
Handling exclusions utilizing try-catch block
We have to set try catch block around that code that might create exception. In bellow provided illustration, System.out.println(a); Is created Deliberately to create an exception.
If you see, that declaration Is composed Inside try block therefore If that declaration throw an exception – catch block may caught and manage It.
If you will operate above illustration In your eclipse, attempt block may be executed.
“Before Exclusion” statement may be published In console and Then statement will create an exclusion so “After Exclusion” statement will be maybe not printed In games console and control will goes to catch prevent to handle that exception.
Click also here: https://github.com/StackExchange/unikong/tree/gh-pages/assets
Constantly use try catch block to log your exception In selenium webdriver reports.
Managing exceptions using throws keyword
Another way of handling exclusion Is using throws keyword with method as shown In holler provided illustration
. Supposing you might have a throwexc method which Is tossing some exclusion and this method Is called from various other process catchexc. Today you desires to handle exclusion of throwexc approach In to catchexc strategy then you have to use punches key word with throwexc procedure.
Using throw Keyword To Toss An Exclusion Expressly
Huge difference between throw and tosses In java :
As we learned, punches keyword Is beneficial to throw these exceptions to contacting processes that aren’t handled Inside called processes. Throw keyword h AS an alternate work – toss key word Is useful to throw an exception expressly whenever required. That Is the variation between throw and throws In java. Interviewer may ask this issue. Why don’t we look at practical example.
In preceding example, ArrayIndexOutOfBoundsException exception will be thrown by toss key word of throwexc procedure.
finally keyword and Its use
Ultimately key word Is used with try catch block at the end of try catch prevent. Code written Inside finally block will be executed always regardless of exclusion Is happened or perhaps not. Chief intent of utilizing finally with try catch prevent Is : If you would wants to perform some action without considering exclusion occurred or perhaps not. finally block will be performed In both the circumstances. Let us see simple illustration of finally.
In preceding instance, 2 try-catch-finally obstructs utilized. Operate above instance and detect outcome.
1st try prevent may toss a mistake and catch will manage and then eventually prevent will be executed. 2nd try block WOn’t throw any error so capture will be perhaps not executed but ultimately block may be executed. Therefore In both the cases ultimately block will be executed.