Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Exp HAndling

Uploaded by

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

Exp HAndling

Uploaded by

arslaan.rj122236
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Name: Areeba Sundal

Registration No: SP22-BSE-021


Assignment: Lab
Submitted to: Mam Samia Zafar
QUESTION#01:
import java.util.InputMismatchException;
import java.util.Scanner;

class NegativeNumberException extends Exception {


public NegativeNumberException() {
super("Cannot calculate square root of a negative number.");
}
}
public class SquareRootCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

try {
System.out.print("Enter a number: ");
double number = scanner.nextDouble();

if (number < 0) {
throw new NegativeNumberException();
}

double squareRoot = Math.sqrt(number);


System.out.println("Square root: " + squareRoot);

} catch (NegativeNumberException e) {
System.out.println("Exception caught: " + e.getMessage());
} catch (InputMismatchException e) {
System.out.println("Invalid input. Please enter a valid number.");
}

scanner.close();
}
}

QUESTION#02:
package task2;
import java.util.Scanner;

class AgeOutOfRangeException extends Exception {


public AgeOutOfRangeException() {
super("You are older than the requested age (25 years)");
}
}

class LowGpaException extends Exception {


public LowGpaException() {
super("Your GPA is not sufficient to apply for this job (2.5)");
}
}

public class JobApplication {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

try {
System.out.print("Enter your age: ");
int applicantAge = scanner.nextInt();

try {
System.out.print("Enter your GPA: ");
double applicantGpa = scanner.nextDouble();
if (applicantAge > 25) {
throw new AgeOutOfRangeException();
}

if (applicantGpa < 2.5) {


throw new LowGpaException();
}

System.out.println("Your application is accepted and is under study");

} catch (LowGpaException e) {
System.out.println("Exception caught: " + e.getMessage());
}

} catch (AgeOutOfRangeException e) {
System.out.println("Exception caught: " + e.getMessage());
} catch (Exception e) {
System.out.println("Invalid input. Please enter valid values.");
}

scanner.close();
}
}

Lab Tasks
Question No: 1
import java.util.InputMismatchException;
import java.util.Scanner;

class NegativeNumberException extends Exception {


public NegativeNumberException() {
super("Cannot calculate square root of a negative number.");
}
}

public class SquareRootCalculator {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

try {
System.out.print("Enter a number: ");
double number = scanner.nextDouble();

if (number < 0) {
throw new NegativeNumberException();
}

double squareRoot = Math.sqrt(number);


System.out.println("Square root: " + squareRoot);

} catch (NegativeNumberException e) {
System.out.println("Exception caught: " + e.getMessage());
} catch (InputMismatchException e) {
System.out.println("Invalid input. Please enter a valid number.");
}

scanner.close();
}
}

Question No: 2
package task2;
import java.util.Scanner;

class AgeOutOfRangeException extends Exception {


public AgeOutOfRangeException() {
super("You are older than the requested age (25 years)");
}
}

class LowGpaException extends Exception {


public LowGpaException() {
super("Your GPA is not sufficient to apply for this job (2.5)");
}
}

public class JobApplication {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try {
System.out.print("Enter your age: ");
int age = scanner.nextInt();

try {
System.out.print("Enter your GPA: ");
double gpa = scanner.nextDouble();

if (age > 25) {


throw new AgeOutOfRangeException();
}

if (gpa < 2.5) {


throw new LowGpaException();
}

System.out.println("Your application is accepted and is under study");

} catch (LowGpaException e) {
System.out.println("Exception caught: " + e.getMessage());
}

} catch (AgeOutOfRangeException e) {
System.out.println("Exception caught: " + e.getMessage());
} catch (Exception e) {
System.out.println("Invalid input. Please enter valid values.");
}
scanner.close();
}
}

You might also like