Java Basics - Week4 - Day 2
~30 mins
Week 4 - Day 2
package week4.day2;
public class LearnExceptionHandling {
public static void main(String[] args) {
int x = 10;
int y = 5;
int z;
int[] num = { 10, 20, 30 }; // indices 0,1,2
try {
z = x / y;
System.out.println(z);
try {
System.out.println(num[3]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e);
}
} catch (ArithmeticException e) {
System.out.println(e);
} catch (Exception e) {
System.out.println(e);
}
System.out.println("End of the program");
}
}
package week4.day2;
import java.io.IOException;
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.ElementNotInteractableException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
public class LearnFinallyBlock {
public static void main(String[] args) throws InterruptedException, IOException {
int x = 10;
int y = 0;
int z;
try {
z = x / y;
System.out.println(z);
} finally {
Runtime.getRuntime().exec("taskkill /f /im chromedriver.exe");
}
System.out.println("End of the program");
}
}
package week4.day2;
import java.io.IOException;
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.ElementNotInteractableException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
public class LearnThowKeyword {
public int divide(int x, int y) {
int z = 0;
if(x > y) {
z = x / y ;
}
else {
throw new ArithmeticException("Invalid input");
}
return z;
}
public static void main(String[] args) {
LearnThowKeyword ltk = new LearnThowKeyword();
try {
System.out.println(ltk.divide(5, 10));
} catch (Exception e) {
System.out.println(e);
}
System.out.println("End of the program");
}
}
package week4.day2;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.ElementNotInteractableException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
public class LearnThows {
public static void main(String[] args) throws FileNotFoundException {
File file = new File("C:\\Users\\hp\\Desktop\\NewData.csv");
FileInputStream fis = new FileInputStream(file);
System.out.println("end of the program");
}
}