0% found this document useful (0 votes)
4 views9 pages

Java3 Assignment

The document contains multiple Java classes demonstrating exception handling, including arithmetic exceptions, array index out of bounds, and custom exceptions for vowel checks, odd number checks, and birth year validation. Each class includes a main method that prompts user input and handles exceptions accordingly. The examples illustrate the use of try-catch blocks and the creation of custom exception classes.

Uploaded by

crasssharma
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)
4 views9 pages

Java3 Assignment

The document contains multiple Java classes demonstrating exception handling, including arithmetic exceptions, array index out of bounds, and custom exceptions for vowel checks, odd number checks, and birth year validation. Each class includes a main method that prompts user input and handles exceptions accordingly. The examples illustrate the use of try-catch blocks and the creation of custom exception classes.

Uploaded by

crasssharma
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

1.

class Exc2 {

public sta c void main(String args[]) {

int d, a;

try { // monitor a block of code.

d = 0;

a = 42 / d;

[Link]("This will not be printed.");

} catch (Arithme cExcep on e)

{ // catch divide-by-zero error

[Link]("Division by zero.");

[Link]("A er catch statement.");

1.

class Mul Catch {


public sta c void main(String args[]) {

try {

int a = [Link];

[Link]("a = " + a);

int b = 42 / a;

int c[] = { 1 };

c[42] = 99;

} catch(Arithme cExcep on e) {

[Link]("Divide by 0: " + e);

} catch(ArrayIndexOutOfBoundsExcep on e) {

[Link]("Array index oob: " + e);

[Link]("A er try/catch blocks.");

1.

class ThrowDemo {

sta c void demoproc() {

try {

throw new NullPointerExcep on("demo");


} catch(NullPointerExcep on e)

[Link]("Caught inside demoproc.");

throw e; // rethrow the excep on

public sta c void main(String args[]) {

try { demoproc();

} catch(NullPointerExcep on e)

{ [Link]("Recaught: " + e); }

}}

1.

class ThrowsDemo {

sta c void throwOne() throws IllegalAccessExcep on

[Link]("Inside throwOne.");

throw new IllegalAccessExcep on("demo");

public sta c void main(String args[]) {

try {
throwOne();

catch (IllegalAccessExcep on e) {

[Link]("Caught " + e);

2.

import java.u [Link];

// Custom Excep on Class

class NoVowelExcep on extends Excep on {

public NoVowelExcep on(String message) {

super(message);

public class VowelCheck {

// Method to check vowels

public sta c void checkVowels(String str) throws NoVowelExcep on {

if (str == null || [Link]()) {

throw new NoVowelExcep on("String is empty. No vowels found.");


}

boolean hasVowel = false;

str = [Link](); // Convert to lowercase for easy checking

for (int i = 0; i < [Link](); i++) {

char ch = [Link](i);

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {

hasVowel = true;

break;

if (!hasVowel) {

throw new NoVowelExcep on("String does not contain any vowels.");

[Link]("String contains at least one vowel.");

public sta c void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter a string: ");

String input = [Link]();

try {

checkVowels(input);

} catch (NoVowelExcep on e) {

[Link]("Excep on caught: " + [Link]());

}
[Link]();

3.

import java.u [Link];

// Custom Excep on Class

class OddNumberExcep on extends Excep on {

public OddNumberExcep on(String message) {

super(message);

public class NumberCheck {

// Method to check if number is odd

public sta c void checkNumber(int num) throws OddNumberExcep on {

if (num % 2 != 0) {

throw new OddNumberExcep on("Number is odd. Excep on thrown!");

[Link]("Number is even.");

}
public sta c void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter an integer: ");

int number = [Link]();

try {

checkNumber(number);

} catch (OddNumberExcep on e) {

[Link]("Excep on caught: " + [Link]());

[Link]();

4.

// Custom Excep on Class

class InvalidBirthYearExcep on extends Excep on {

public InvalidBirthYearExcep on(String message) {

super(message);

}
public class BirthYearCheck {

// Method to validate birth year

public sta c void checkBirthYear(int year) throws InvalidBirthYearExcep on {

int currentYear = 2026; // You can also get current year dynamically

if (year > currentYear) {

throw new InvalidBirthYearExcep on("Birth year cannot be in the future.");

if (year < 1900) {

throw new InvalidBirthYearExcep on("Birth year is too old to be valid.");

[Link]("Valid Birth Year.");

public sta c void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter your birth year: ");

int year = [Link]();

try {

checkBirthYear(year);

} catch (InvalidBirthYearExcep on e) {

[Link]("Excep on caught: " + [Link]());

[Link]();

}
}

You might also like