# UNIT 4 – JAVA EXCEPTION HANDLING (Q1 – Q25)
---
## Q1. Basic Try-Catch
Program:
public class Q1 {
public static void main(String[] args) {
try {
int a = 10, b = 0;
int c = a / b;
[Link](c);
} catch (ArithmeticException e) {
[Link]("Cannot divide by zero");
} finally {
[Link]("Program completed");
}
}
}
Output:
Cannot divide by zero
Program completed
---
## Q2. Multiple Exception Handling
Program:
public class Q2 {
public static void main(String[] args) {
try {
int arr[] = new int[3];
arr[4] = 10;
int n = [Link]("abc");
} catch (ArrayIndexOutOfBoundsException e) {
[Link]("Array index out of range");
} catch (NumberFormatException e) {
[Link]("Invalid number format");
}
}
}
---
## Q3. Nested Try-Catch
Program:
public class Q3 {
public static void main(String[] args) {
try {
try {
int x = 10 / 0;
} catch (ArithmeticException e) {
[Link]("Inner catch: divide by zero");
}
int arr[] = new int[2];
[Link](arr[5]);
} catch (ArrayIndexOutOfBoundsException e) {
[Link]("Outer catch: array index out of range");
}
}
}
---
## Q4. User-Defined Exception – InvalidAgeException
Program:
class InvalidAgeException extends Exception {
InvalidAgeException(String msg) { super(msg); }
}
public class Q4 {
static void checkAge(int age) throws InvalidAgeException {
if (age < 18)
throw new InvalidAgeException("Age below 18 not allowed");
}
public static void main(String[] args) {
try { checkAge(15); }
catch (InvalidAgeException e) { [Link]([Link]()); }
}
}
---
## Q5. Manual Exception using throw
Program:
public class Q5 {
static void validate(int marks) {
if (marks < 0)
throw new IllegalArgumentException("Negative marks not valid");
}
public static void main(String[] args) {
validate(-5);
}
}
---
## Q6. throws Keyword Example
Program:
import [Link].*;
public class Q6 {
static void readFile() throws IOException {
FileReader fr = new FileReader("[Link]");
[Link]();
}
public static void main(String[] args) {
try {
readFile();
} catch (IOException e) {
[Link]("File not found or error");
}
}
}
---
## Q7. Handle InputMismatchException
Program:
import [Link].*;
public class Q7 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter an integer: ");
try {
int n = [Link]();
[Link]("Number = " + n);
} catch (InputMismatchException e) {
[Link]("Not an integer!");
}
}
}
---
## Q8. Handle FileNotFoundException
Program:
import [Link].*;
public class Q8 {
public static void main(String[] args) {
try {
FileReader fr = new FileReader("[Link]");
[Link]();
} catch (FileNotFoundException e) {
[Link]("File not found");
} catch (IOException e) {
[Link]("I/O error");
} finally {
[Link]("File handling done");
}
}
}
---
## Q9. Rethrow an Exception
Program:
public class Q9 {
static void method1() throws Exception {
try {
throw new Exception("Error in method1");
} catch (Exception e) {
[Link]("Handled inside method1");
throw e;
}
}
public static void main(String[] args) {
try {
method1();
} catch (Exception e) {
[Link]("Handled in main");
}
}
}
---
## Q10. Multiple Catch Blocks
Program:
public class Q10 {
public static void main(String[] args) {
try {
int a = 10 / 0;
int arr[] = new int[3];
arr[5] = 20;
} catch (ArithmeticException e) {
[Link]("Division by zero");
} catch (ArrayIndexOutOfBoundsException e) {
[Link]("Array index error");
}
}
}
---
## Q11. Exception Propagation
Program:
public class Q11 {
static void method1() throws Exception { method2(); }
static void method2() throws Exception { throw new Exception("Exception propagated"); }
public static void main(String[] args) {
try { method1(); }
catch (Exception e) { [Link]([Link]()); }
}
}
---
## Q12. Custom Exception – NegativeNumberException
Program:
class NegativeNumberException extends Exception {
NegativeNumberException(String msg) { super(msg); }
}
public class Q12 {
static void check(int n) throws NegativeNumberException {
if (n < 0) throw new NegativeNumberException("Negative number entered");
}
public static void main(String[] args) {
try { check(-5); }
catch (NegativeNumberException e) { [Link]([Link]()); }
}
}
---
## Q13. Division with Finally Block
Program:
public class Q13 {
public static void main(String[] args) {
try {
int a = 10, b = 2;
[Link]("Result = " + (a / b));
} finally {
[Link]("Program executed successfully");
}
}
}
---
## Q14. Handle NullPointerException
Program:
public class Q14 {
public static void main(String[] args) {
String s = null;
try {
[Link]([Link]());
} catch (NullPointerException e) {
[Link]("NullPointerException handled");
}
}
}
---
## Q15. Checked and Unchecked Exception
Program:
import [Link].*;
public class Q15 {
static void checked() throws IOException { throw new IOException("Checked Exception"); }
static void unchecked() { int a = 10 / 0; }
public static void main(String[] args) {
try { checked(); } catch (IOException e) { [Link]([Link]()); }
try { unchecked(); } catch (ArithmeticException e) { [Link]("Unchecked Exception"); }
}
}
---
## Q16. Try Without Catch (with Finally)
Program:
public class Q16 {
public static void main(String[] args) {
try {
[Link]("Inside try block");
} finally {
[Link]("Finally executed");
}
}
}
---
## Q17. Generic Exception Message
Program:
public class Q17 {
public static void main(String[] args) {
try {
String s = null;
[Link]([Link]());
} catch (Exception e) {
[Link]("Error occurred");
}
}
}
---
## Q18. Robust Input Addition
Program:
import [Link].*;
public class Q18 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int a = 0, b = 0;
while (true) {
try {
[Link]("Enter first number: ");
a = [Link]([Link]());
break;
} catch (NumberFormatException e) {
[Link]("Invalid input, try again");
}
}
while (true) {
try {
[Link]("Enter second number: ");
b = [Link]([Link]());
break;
} catch (NumberFormatException e) {
[Link]("Invalid input, try again");
}
}
[Link]("Sum = " + (a + b));
}
}
---
## Q19. What is Exception Handling?
Definition:
Exception handling is a mechanism in Java to handle runtime errors so that normal flow of the
program can be maintained.
Importance:
- Prevents program crash.
- Helps find and fix runtime problems.
- Ensures resource cleanup.
Example:
try {
int a = 10 / 0;
} catch (ArithmeticException e) {
[Link]("Handled");
}
Output:
Handled
---
## Q20. Checked vs Unchecked Exceptions
| Point | Checked Exception | Unchecked Exception |
|--------|------------------|--------------------|
| Checked at | Compile time | Runtime |
| Need to handle? | Must be caught or declared | Not mandatory |
| Package | [Link] | [Link] |
| Examples | IOException, SQLException | NullPointerException, ArithmeticException |
Example:
try { new FileReader("[Link]"); } catch (IOException e) {}
int a = 10 / 0; // Unchecked exception