Java for Selenium Part 7

Java Exception Handling, File Handling

i) Java Exception Handling

ii) File Handling inwards Java
--------------------------------------------------
i) Java Exception Handling

> An Exception is an lawsuit that occurs during execution of a plan when normal execution of a plan is interrupted.

> Exception treatment is a machinery to grip exceptions.

Common Scenarios where exceptions may occur
--------------------------------------------------
1) Scenario where ArithemeticException occurs
If nosotros dissever whatever give away past times Zero too therefore ArithemeticException occurs

Ex:

int a=10/0;
--------------------------------------------------
2) Scenario where NullPointerException occurs

if nosotros accept no value inwards whatever variable, performing operations past times the variable.

ex:

String sec = null;

System.out.prinln(s.length());//NullPointerException
--------------------------------------------------
3) Scenario where NumberFormatException occurs
The incorrect formatting of whatever value.

String sec ="abcd";

int a = Integer.parseInt(s);
System.out.println(a);
------------------------------------
Scanner scan = novel Scanner(System.in);
System.out.println("Read Two Number");
String s1 = scan.nextLine();
String s2 = scan.nextLine();

int a = Integer.parseInt(s1);
int b = Integer.parseInt(s2);
System.out.println(a+b);
--------------------------------------------------
When nosotros require to convert the data?

Whenever nosotros read information (any type) too therefore figurer plan considers the information equally String type data, inwards gild to perform mathematical operations too therefore nosotros require convert the data.
----------------------------------------
4) Scenario where ArrayIndexOutofBounds exception occurs

if nosotros assign whatever value inwards the incorrect index too therefore nosotros perish ArrayIndexOutofBounds exception

ex:

int [] a = novel int[5];

a[10] = 123;

System.out.println(a[10]);//ArrayIndexOutofBounds
--------------------------------------------------
Example:

int a=10;
int b =0;
int effect = a/b;

System.out.println(result);
System.out.println("Hello Java");
System.out.println("Hello Selenium");
   
}
}

Use endeavor grab block

Syntax:

try
{
Statements
------------
------------
}
catch (Exception exceptionName){
Exception Handling code;
}

Example:

public static void main(String[] args) {
int a=10;
int b =0;

try
{
int effect = a/b;
System.out.println(result);
}
catch (ArithmeticException e){
System.out.println("Diveded past times Zero Error");   
}
System.out.println("Hello Java");
System.out.println("Hello Selenium");
}
---------------------------------------------------
//Handling multiple Exceptions

int a=10;
int b =0;
int effect = a/b;

int [] c = novel int [4];
c[10] = 100;

System.out.println(result);
System.out.println(c[100]);
System.out.println("Hello Java");
System.out.println("Hello Selenium");
       
}
}
-----------------------------------------------
public static void main(String[] args) {
int a=10;
int b =0;

try
{
int effect = a/b;
System.out.println(result);
}
catch (ArithmeticException e1){
System.out.println("Diveded past times Zero Error");   
}
int [] c = novel int [4];
try
{
c[10] = 100;
System.out.println(c[10]);
}
catch (ArrayIndexOutOfBoundsException e2){
System.out.println("Array Index Error");
}
System.out.println("Hello Java");
System.out.println("Hello Selenium");
       
}
}
--------------------------------------------------
ii) File Handling inwards Java
Using File Class nosotros tin grip Computer files

Using File Object nosotros tin grip all types files (Ex: text files/excel files etc...) but external operations only.

External operations

Create File

Delete File etc...

Internal operations (Reading, Writing etc...) alone for Text files.
------------------------------------------
Examples:

1) Create a Folder

File fileObject = novel File("C:/Users/gcreddy/Desktop/Selenium");
fileObject.mkdir();
----------------------------------------------
2) Check the being of Selenium Folder
File fileObject = novel File("C:/Users/gcreddy/Desktop/Selenium");
boolean a = fileObject.exists();

if (a == true){
System.out.println("Folder Exists");
}
else {
System.out.println("Folder Not Exists");   
}
-------------------------------------------
File fileObject = novel File("C:/Users/gcreddy/Desktop/abcd.xlsx");
boolean a = fileObject.exists();

if (a == true){
System.out.println("File Exists");
}
else {
System.out.println("File Not Exists");   
}
-------------------------------------------------
4) Delete a Folder
File fileObject = novel File("C:/Users/gcreddy/Desktop/abc");
fileObject.delete();
------------------------------------------------
5) Create a Text File
File fileObject = novel File("C:/Users/gcreddy/Desktop/UFT.doc");
fileObject.createNewFile();
-----------------------------------------------------------------
File fileObject = novel File("C:/Users/gcreddy/Desktop/UFT.cpp");
try {
fileObject.createNewFile();
} grab (IOException e) {
e.printStackTrace();
}
----------------------------------------------
6) Delete a Text File
File fileObject = novel File("C:/Users/gcreddy/Desktop/UFT.txt");
fileObject.delete();
--------------------------------------------------
7) Read a Text File

String line;
FileReader file = novel FileReader("C:/Users/gcreddy/Desktop/UFT.txt");
BufferedReader br = novel BufferedReader(file);

while ((line = br.readLine()) !=null){
System.out.println(line);   
}
br.close();
file.close();
}
------------------------------------------------
8) Write Data to a Text File

FileWriter file = novel FileWriter("C:/Users/gcreddy/Desktop/UFT.txt");
BufferedWriter bw = novel BufferedWriter(file);

String information = "Welcome to Java";
bw.write(data);
bw.close();
file.close();
----------------------------------------------
9) Read information from UFT.txt file too Write to Selenium.txt file

String line;
FileReader file1 = novel FileReader("C:/Users/gcreddy/Desktop/UFT.txt");
FileWriter file2 = novel FileWriter("C:/Users/gcreddy/Desktop/Selenium.txt");

BufferedReader br = novel BufferedReader(file1);
BufferedWriter bw = novel BufferedWriter(file2);

while ((line =br.readLine()) != null){
bw.write(line);   
bw.newLine();
}
br.close();
bw.close();
file1.close();
file2.close();
--------------------------------------------------

Sumber http://www.gcreddy.com/
Post a Comment (0)
Previous Post Next Post