1. Division Program :
public class DivisionTest {
public static void main(String[] args) {
int a = 10;
int b = 0;
int result = a / b;
System.out.println("Result: " + result);
}
}
Handled Exception Source code:
public class DivisionTest {
public static void main(String[] args) {
int a = 10;
int b = 0;
try {
int result = a / b;
System.out.println("Result: " + result);
}
catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
}
}
}
2. Array Index Program :
public class ArrayTest {
public static void main(String[] args) {
int arr[] = {10, 20, 30, 40};
System.out.println(arr[5]);
}
}
Handled Exception Source code:
public class ArrayTest {
public static void main(String[] args) {
try {
int arr[] = {10, 20, 30, 40};
System.out.println(arr[5]);
} catch (ArrayIndexOutOfBoundsException e) {
//System.out.println("Index 5 out of bounds for length 4");
System.out.println(e.getMessage());
}
}
}
3. String Conversion Program :
public class NumberFormatTest {
public static void main(String[] args) {
String s = "ABC";
int num = Integer.parseInt(s);
System.out.println(num);
}
}
Handled Exception Source Code
public class NumberFormatTest {
public static void main(String[] args) {
try {
String s = "ABC";
int num = Integer.parseInt(s);
System.out.println(num);
}
catch(NumberFormatException e){
System.out.println("Invalid number format. Cannot convert string to integer.");
}
}
}
4. NullPointer Program :
public class NullTest {
public static void main(String[] args) {
String name = null;
System.out.println(name.length());
}
}
Handled Exception Source Code
public class NullTest {
public static void main(String[] args) {
try{
String name = null;
System.out.println(name.length());
}
catch(NullPointerException e){
System.out.println(e.getMessage());
}
}
}
5. File Reading Program :
public class FileTest {
public static void main(String[] args) throws Exception {
FileReader fr = new FileReader("test.txt");
int data = fr.read();
System.out.println(data);
}
}
Handled Exception Source Code
import java.io.FileReader;
import java.io.IOException;
public class FileTest {
public static void main(String[] args) {
try {
FileReader fr = new FileReader("test.txt");
int data = fr.read();
System.out.println(data);
}
catch (IOException e) {
System.out.println("File not found");
}
}
}
6. String Index Program :
public class StringIndexTest {
public static void main(String[] args) {
String str = "Java";
System.out.println(str.charAt(10));
}
}
Handled Exception Source Code
public class StringIndexTest {
public static void main(String[] args) {
try {
String str = "Java";
System.out.println(str.charAt(10));
} catch (StringIndexOutOfBoundsException e) {
System.out.println(e.getMessage());
}
}
}
7. Class Casting Program :
public class CastingTest {
public static void main(String[] args) {
Object obj = new Integer(10);
String s = (String) obj;
System.out.println(s);
}
}
Handled Exception Source Code
public class CastingTest {
public static void main(String[] args) {
try {
Object obj = new Integer(10);
String s = (String) obj;
System.out.println(s);
}
catch (ClassCastException e) {
System.out.println("Cannot cast Integer to String.");
}
}
}
8. Negative Array Size :
public class NegativeArrayTest {
public static void main(String[] args) {
int size = -5;
int arr[] = new int[size];
}
}
Handled Exception Source Code
public class NegativeArrayTest {
public static void main(String[] args) {
try {
int size = -5;
int arr[] = new int[size];
}
catch (NegativeArraySizeException e) {
System.out.println("Array size cannot be negative");
}
}
}
9.TreeSet Programs :
public class Test
{
public static void main(String[] args) {
TreeSet set = new TreeSet();
set.add(10);
set.add(30);
set.add("hari");
System.out.println(set);
}
}
Handled Exception Source Code
import java.util.TreeSet;
public class Test {
public static void main(String[] args) {
try {
TreeSet set = new TreeSet();
set.add(10);
set.add(30);
set.add("hari");
System.out.println(set);
}
catch (ClassCastException e) {
System.out.println("Different data types are not allowed in TreeSet.");
}
}
}
10.TreeSet programs :
public class Test
{
public static void main(String[] args) {
TreeSet set = new TreeSet();
set.add(20);
set.add(40);
set.add(null);
}
}
Handled Exception Source Code
import java.util.TreeSet;
public class Test {
public static void main(String[] args) {
try {
TreeSet set = new TreeSet();
set.add(20);
set.add(40);
set.add(null);
System.out.println(set);
}
catch (NullPointerException e) {
System.out.println("Null values are not allowed in TreeSet.");
}
}
}
Top comments (0)