0% found this document useful (0 votes)
2 views3 pages

8 Java

The document contains Java code for two classes, DivisionCheck and Test, which handle exceptions related to division and age validation, respectively. The DivisionCheck class throws an exception for division by zero and for results less than 0.01, while the Test class checks for negative age values. Both classes demonstrate exception handling using custom exception classes.

Uploaded by

tejaspol007
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

8 Java

The document contains Java code for two classes, DivisionCheck and Test, which handle exceptions related to division and age validation, respectively. The DivisionCheck class throws an exception for division by zero and for results less than 0.01, while the Test class checks for negative age values. Both classes demonstrate exception handling using custom exception classes.

Uploaded by

tejaspol007
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

You might also like