Java for Selenium Part 7

Java Exception Handling, File Handling

i) Java Exception Handling

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

> An Exception is an event that occurs during execution of a program when normal execution of a program is interrupted.

> Exception handling is a mechanism to handle exceptions.

Common Scenarios where exceptions may occur
--------------------------------------------------
1) Scenario where ArithemeticException occurs
If we divide any number by Zero then ArithemeticException occurs

Ex:

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

if we have no value in any variable, performing operations by the variable.

ex:

String s = null;

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

String s ="abcd";

int a = Integer.parseInt(s);
System.out.println(a);
------------------------------------
Scanner scan = new 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 we need to convert the data?

Whenever we read data (any type) then computer program considers the data as String type data, in order to perform mathematical operations then we need convert the data.
----------------------------------------
4) Scenario where ArrayIndexOutofBounds exception occurs

if we assign any value in the wrong index then we get ArrayIndexOutofBounds exception

ex:

int [] a = new int[5];

a[10] = 123;

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

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

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

Use try catch 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 result = a/b;
System.out.println(result);
}
catch (ArithmeticException e){
System.out.println("Diveded by Zero Error");   
}
System.out.println("Hello Java");
System.out.println("Hello Selenium");
}
---------------------------------------------------
//Handling multiple Exceptions

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

int [] c = new 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 result = a/b;
System.out.println(result);
}
catch (ArithmeticException e1){
System.out.println("Diveded by Zero Error");   
}
int [] c = new 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 in Java
Using File Class we can handle Computer files

Using File Object we can handle 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...) only for Text files.
------------------------------------------
Examples:

1) Create a Folder

File fileObject = new File("C:/Users/gcreddy/Desktop/Selenium");
fileObject.mkdir();
----------------------------------------------
2) Check the existence of Selenium Folder
File fileObject = new 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 = new 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 = new File("C:/Users/gcreddy/Desktop/abc");
fileObject.delete();
------------------------------------------------
5) Create a Text File
File fileObject = new File("C:/Users/gcreddy/Desktop/UFT.doc");
fileObject.createNewFile();
-----------------------------------------------------------------
File fileObject = new File("C:/Users/gcreddy/Desktop/UFT.cpp");
try {
fileObject.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
----------------------------------------------
6) Delete a Text File
File fileObject = new File("C:/Users/gcreddy/Desktop/UFT.txt");
fileObject.delete();
--------------------------------------------------
7) Read a Text File

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

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

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

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

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

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

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

0 comments:

Post a Comment