import java.u l.
Scanner;
class SmallResultExcep on extends Excep on {
public SmallResultExcep on(String message) {
super(message);
}
}
public class DivisionCheck {
sta c void checkDivision(double a, double b) throws SmallResultExcep on {
if (b == 0) {
throw new Arithme cExcep on("Cannot divide by zero!");
}
double result = a / b;
if (result < 0.01) {
throw new SmallResultExcep on("Result is less than 0.01!");
} else {
[Link]("Result = " + result);
}
}
public sta c void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
double num1 = [Link]();
[Link]("Enter second number: ");
double num2 = [Link]();
try {
checkDivision(num1, num2);
} catch (SmallResultExcep on e) {
[Link]("Excep on: " + [Link]());
} catch (Arithme cExcep on e) {
[Link]([Link]());
}
[Link]();
}
}
OUTPUT
C:\Users\visha>cd Desktop/JAVA
C:\Users\visha\Desktop\JAVA>java [Link]
Enter first number: 10
Enter second number: 0
Cannot divide by zero!
class InvalidAgeExcep on extends Excep on {
public InvalidAgeExcep on(String message) {
super(message);
}
}
public class Test {
sta c void checkAge(int age) throws InvalidAgeExcep on {
if (age < 0) {
throw new InvalidAgeExcep on("Age cannot be nega ve");
} else {
[Link]("Valid Age");
}
}
public sta c void main(String[] args) {
try {
checkAge(-5);
} catch (InvalidAgeExcep on e) {
[Link]("Excep on caught: " + [Link]());
}
}
}
OUTPUT
C:\Users\visha\Desktop\JAVA>java [Link]
Excep on caught: Age cannot be nega ve