Assignment5
Assignment5
**Syntax:**
try {
// Code that may cause an exception
} catch (ExceptionType e) {
// Handling the exception
} finally {
// Code that always executes
**Example:**
```java
public class ExceptionExample {
public static void main(String[] args) {
try {
int result = 10 / 0; // Causes ArithmeticException
} catch (ArithmeticException e) {
System.out.println("Exception caught: " + e);
} finally {
System.out.println("Finally block executed.");
}
}
}
```
**Example:**
try {
System.out.println("Inside try block");
} catch (Exception e) {
System.out.println("Exception caught");
} finally {
System.out.println("Finally block executed.");
}
3.Provide a suitable example to demonstrate Exception Handling in Java.
**Example:**
public class ThrowExample {
static void checkAge(int age) {
if (age < 18) {
throw new ArithmeticException("Not eligible to vote");
} else {
System.out.println("Eligible to vote");
}
}