![Practice 70 Java Exception Handling Programming Questions, TechnoVlogs](admin/uploads/blog/67a1e94e1aa408.09309337.webp)
Practice 70 Java Exception Handling Programming Questions
Q1. Write a Java program to handle an ArithmeticException when dividing by zero.
Input:
a = 10, b = 0
Expected Output:
ArithmeticException: Division by zero is not allowed.
Q2. Write a Java program to handle a NullPointerException when trying to call a method on a null object.
Input:
object = null
Expected Output:
NullPointerException: Cannot invoke method on a null object.
Q3. Write a Java program to handle an ArrayIndexOutOfBoundsException when accessing an invalid array index.
Input:
array = [1, 2, 3], index = 5
Expected Output:
ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 3.
Q4. Write a Java program to handle a NumberFormatException when parsing an invalid integer.
Input:
str = "abc"
Expected Output:
NumberFormatException: For input string: "abc"
Q5. Write a Java program to handle a FileNotFoundException when trying to open a file that does not exist.
Input:
File path: "nonexistent.txt"
Expected Output:
FileNotFoundException: The file does not exist.
Q6. Write a Java program to handle an IOException while reading a file.
Input:
File path: "testfile.txt" (Assume the file is not readable)
Expected Output:
IOException: An error occurred while reading the file.
Q7. Write a Java program to handle a ClassNotFoundException when trying to load a non-existing class.
Input:
Class name: "NonExistentClass"
Expected Output:
ClassNotFoundException: Class NonExistentClass not found.
Q8. Write a Java program to handle a NegativeArraySizeException when trying to create an array with a negative size.
Input:
Array size: -5
Expected Output:
NegativeArraySizeException: Size cannot be negative.
Q9. Write a Java program to handle a StackOverflowError when there is excessive recursion.
Input:
Recursion depth: 10000
Expected Output:
StackOverflowError: Too many recursive calls.
Q10. Write a Java program to handle a IllegalArgumentException when an invalid argument is passed to a method.
Input:
Method argument: -1
Expected Output:
IllegalArgumentException: Argument cannot be negative.
Q11. Write a Java program to handle a ConcurrentModificationException when modifying a collection while iterating.
Input:
List = [1, 2, 3], iterator.next(), list.remove(0)
Expected Output:
ConcurrentModificationException: Collection was modified while iterating.
Q12. Write a Java program to handle a TimeoutException when a process exceeds the allowed time limit.
Input:
Timeout: 5 seconds
Expected Output:
TimeoutException: Process exceeded time limit.
Q13. Write a Java program to handle a SQLException when there is a problem with database connectivity.
Input:
Database connection: Invalid credentials
Expected Output:
SQLException: Unable to establish database connection.
Q14. Write a Java program to handle an IllegalStateException when performing an operation in an inappropriate state.
Input:
State: "Inactive", Operation: "Start"
Expected Output:
IllegalStateException: Cannot perform operation in current state.
Q15. Write a Java program to handle a NoSuchElementException when trying to access an element from an empty collection.
Input:
Collection = []
Expected Output:
NoSuchElementException: No element found in the collection.
Q16. Write a Java program to handle a InterruptedException when a thread is interrupted.
Input:
Thread sleep: 1000 ms, Thread interruption
Expected Output:
InterruptedException: Thread was interrupted.
Q17. Write a Java program to handle an ArithmeticException for division by zero using try-catch block.
Input:
a = 10, b = 0
Expected Output:
Cannot divide by zero.
Q18. Write a Java program to handle a FileNotFoundException while attempting to read from a file.
Input:
File path: "nonexistent.txt"
Expected Output:
FileNotFoundException: File does not exist.
Q19. Write a Java program to handle an ArrayIndexOutOfBoundsException when an index exceeds array bounds.
Input:
Array: [1, 2, 3], index = 5
Expected Output:
ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 3.
Q20. Write a Java program to handle an NullPointerException when dereferencing a null object.
Input:
Object: null
Expected Output:
NullPointerException: Cannot access method on null object.
Q21. Write a Java program to handle a NumberFormatException when attempting to parse a non-numeric string to an integer.
Input:
String: "abc123"
Expected Output:
NumberFormatException: For input string: "abc123"
Q22. Write a Java program to handle a ClassCastException when attempting to cast an object to a wrong type.
Input:
Object: String, Cast to: Integer
Expected Output:
ClassCastException: Cannot cast java.lang.String to java.lang.Integer
Q23. Write a Java program to handle an IndexOutOfBoundsException when accessing an invalid list index.
Input:
List = [10, 20, 30], Index = 5
Expected Output:
IndexOutOfBoundsException: Index 5 is out of bounds.
Q24. Write a Java program to handle a ParseException when parsing a date in an invalid format.
Input:
Date string: "32/13/2025"
Expected Output:
ParseException: Unparseable date: "32/13/2025"
Q25. Write a Java program to handle an IllegalArgumentException when passing an invalid argument to a method.
Input:
Argument: -100
Expected Output:
IllegalArgumentException: Invalid argument. Must be positive.
Q26. Write a Java program to handle a FileNotFoundException when trying to open a non-existing file.
Input:
File path: "missing_file.txt"
Expected Output:
FileNotFoundException: File not found.
Q27. Write a Java program to handle an NumberFormatException when attempting to parse a string with non-numeric characters.
Input:
String: "123abc"
Expected Output:
NumberFormatException: For input string: "123abc"
Q28. Write a Java program to handle an IOException when reading from a file that is not accessible.
Input:
File path: "restricted_file.txt"
Expected Output:
IOException: Unable to read the file.
Q29. Write a Java program to handle a NoSuchElementException when attempting to retrieve an element from an empty list.
Input:
List: []
Expected Output:
NoSuchElementException: No such element found.
Q30. Write a Java program to handle a ConcurrentModificationException while modifying a collection during iteration.
Input:
List: [1, 2, 3], Operation: remove during iteration
Expected Output:
ConcurrentModificationException: List structure modified during iteration.
Q31. Write a Java program to handle a TimeoutException when a task exceeds the allowed execution time.
Input:
Task execution time: 10 seconds, Timeout: 5 seconds
Expected Output:
TimeoutException: Task exceeded the allowed time limit.
Q32. Write a Java program to handle an ArrayStoreException when trying to store an incompatible type in an array.
Input:
Array: Integer[], Value: "string"
Expected Output:
ArrayStoreException: Incompatible type in array.
Q33. Write a Java program to handle a SecurityException when a security violation occurs.
Input:
Action: Unauthorized file access
Expected Output:
SecurityException: Access denied.
Q34. Write a Java program to handle a FileNotFoundException when trying to open a file with invalid path.
Input:
File path: "invalid_path.txt"
Expected Output:
FileNotFoundException: Invalid file path.
Q35. Write a Java program to handle an IllegalArgumentException when an invalid parameter is passed.
Input:
Parameter: -5
Expected Output:
IllegalArgumentException: Argument cannot be negative.
Q36. Write a Java program to handle an IOException when attempting to write to a file without proper permission.
Input:
File path: "readonly.txt"
Expected Output:
IOException: Cannot write to read-only file.
Q37. Write a Java program to handle a FileNotFoundException when attempting to open a non-existent file.
Input:
File path: "nonexistent_file.txt"
Expected Output:
FileNotFoundException: The file does not exist.
Q38. Write a Java program to handle a ClassNotFoundException when loading a class that is not found.
Input:
Class name: "UnknownClass"
Expected Output:
ClassNotFoundException: Class UnknownClass not found.
Q39. Write a Java program to handle an ArrayIndexOutOfBoundsException when an index is out of bounds.
Input:
Array size: 3, Index: 5
Expected Output:
ArrayIndexOutOfBoundsException: Index out of bounds.
Q40. Write a Java program to handle an NullPointerException when accessing an object that is null.
Input:
Object: null
Expected Output:
NullPointerException: Cannot access object.
Q41. Write a Java program to handle an ArithmeticException when performing a division by zero.
Input:
a = 25, b = 0
Expected Output:
ArithmeticException: Cannot divide by zero.
Q42. Write a Java program to handle an IllegalStateException when an action is performed in an invalid state.
Input:
State: "Inactive", Action: "Start"
Expected Output:
IllegalStateException: Action cannot be performed in inactive state.
Q43. Write a Java program to handle a FileNotFoundException when attempting to access a missing file.
Input:
File path: "missing.txt"
Expected Output:
FileNotFoundException: File not found.
Q44. Write a Java program to handle an ArithmeticException when dividing by zero.
Input:
a = 5, b = 0
Expected Output:
ArithmeticException: Cannot divide by zero.
Q45. Write a Java program to handle an ArrayIndexOutOfBoundsException when an invalid index is used.
Input:
Array: [1, 2, 3], Index: 4
Expected Output:
ArrayIndexOutOfBoundsException: Index 4 out of bounds for length 3.
Q46. Write a Java program to handle a ClassCastException when casting an object to an incompatible type.
Input:
Object: "string", Cast to: Integer
Expected Output:
ClassCastException: Cannot cast java.lang.String to java.lang.Integer
Q47. Write a Java program to handle an NullPointerException when calling a method on a null object.
Input:
Object: null
Expected Output:
NullPointerException: Cannot invoke method on null object.
Q48. Write a Java program to handle a NoSuchElementException when accessing an empty collection.
Input:
Collection: []
Expected Output:
NoSuchElementException: No such element found.
Q49. Write a Java program to handle an ArithmeticException when dividing by zero using try-catch.
Input:
a = 10, b = 0
Expected Output:
Cannot divide by zero.
Q50. Write a Java program to handle a FileNotFoundException when trying to read from a file that doesn't exist.
Input:
File path: "missing.txt"
Expected Output:
FileNotFoundException: File missing.txt not found.
Q51. Write a Java program to handle an IllegalArgumentException when an invalid value is passed to a method.
Input:
Value: -1
Expected Output:
IllegalArgumentException: Invalid argument. Value cannot be negative.
Q52. Write a Java program to handle an IOException when there is a problem reading from a file.
Input:
File: "testfile.txt"
Expected Output:
IOException: Error reading the file.
Q53. Write a Java program to handle a FileNotFoundException when opening a non-existent file.
Input:
File: "abc.txt"
Expected Output:
FileNotFoundException: File abc.txt not found.
Q54. Write a Java program to handle a TimeoutException when an operation takes longer than expected.
Input:
Timeout: 2 seconds
Expected Output:
TimeoutException: Operation timed out after 2 seconds.
Q55. Write a Java program to handle an ArithmeticException during division by zero.
Input:
a = 12, b = 0
Expected Output:
ArithmeticException: Division by zero is not allowed.
Q56. Write a Java program to handle a ClassNotFoundException when trying to load a non-existing class.
Input:
Class name: "UnknownClass"
Expected Output:
ClassNotFoundException: Class UnknownClass could not be loaded.
Q57. Write a Java program to handle a FileNotFoundException when trying to access a file that doesn't exist.
Input:
File: "wrongfile.txt"
Expected Output:
FileNotFoundException: wrongfile.txt not found.
Q58. Write a Java program to handle a ParseException when parsing an invalid date string.
Input:
Date: "2025/13/32"
Expected Output:
ParseException: Invalid date format.
Q59. Write a Java program to handle an IOException when trying to access a file without read permissions.
Input:
File: "readonlyfile.txt"
Expected Output:
IOException: Cannot read from readonlyfile.txt
Q60. Write a Java program to handle a StackOverflowError during recursive function calls.
Input:
Function calls: Recursion depth = 10000
Expected Output:
StackOverflowError: Too many recursive calls.
Q61. Write a Java program to handle a ClassCastException when trying to cast an incompatible object type.
Input:
Object: "text", Type: Integer
Expected Output:
ClassCastException: Cannot cast String to Integer
Q62. Write a Java program to handle a NoSuchMethodException when a method is not found in a class.
Input:
Class: "String", Method: "nonExistingMethod"
Expected Output:
NoSuchMethodException: Method nonExistingMethod not found in String class
Q63. Write a Java program to handle a SecurityException when trying to access restricted resources.
Input:
Action: Accessing a restricted file
Expected Output:
SecurityException: Permission denied.
Q64. Write a Java program to handle a FileNotFoundException when attempting to read from a missing file.
Input:
File path: "missingfile.txt"
Expected Output:
FileNotFoundException: The file missingfile.txt does not exist.
Q65. Write a Java program to handle an ArithmeticException for division by zero using try-catch block.
Input:
a = 50, b = 0
Expected Output:
ArithmeticException: Cannot divide by zero.
Q66. Write a Java program to handle a NegativeArraySizeException when trying to create an array with a negative size.
Input:
Array size: -5
Expected Output:
NegativeArraySizeException: Array size cannot be negative.
Q67. Write a Java program to handle an IndexOutOfBoundsException when accessing an element at an invalid index in an array.
Input:
Array: [1, 2, 3], Index = 4
Expected Output:
IndexOutOfBoundsException: Index 4 is out of bounds for length 3.
Q68. Write a Java program to handle a ClassNotFoundException when trying to load a class that doesn't exist.
Input:
Class name: "NonExistentClass"
Expected Output:
ClassNotFoundException: Class NonExistentClass not found.
Q69. Write a Java program to handle a ClassCastException when attempting to cast an object to an incompatible class.
Input:
Object: Integer, Cast to: String
Expected Output:
ClassCastException: Cannot cast Integer to String
Q70. Write a Java program to handle a NoSuchElementException when attempting to retrieve an element from an empty collection.
Input:
List: []
Expected Output:
NoSuchElementException: No element found in the collection.
![Bikki Singh Instructor TechnoVlogs](assets/img/instructor.webp)
Bikki Singh
Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!