0% found this document useful (0 votes)
25 views7 pages

Lab Exeception

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)
25 views7 pages

Lab Exeception

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/ 7

Name: Arslaan Ejaz

Registration No: SP22-BSE-022


Assignment: Lab
Submitted to: Mam Samia Zafar
Home Tasks
Question No: 1
import java.util.Scanner;

class NegativeNumberException extends Exception {

public NegativeNumberException() {

super("You should enter a positive number");

class Exercise3 {

public static double M1(double number) throws NegativeNumberException {

if (number < 0) {

throw new NegativeNumberException();

return Math.sqrt(number);

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter a number: ");

double input = scanner.nextDouble();

try {

double result = M1(input);

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

} catch (NegativeNumberException e) {

System.out.println("Exception caught: " + e.getMessage());

}
}

Question No: 2
import java.util.InputMismatchException;

import java.util.Scanner;

class NegativeValueException extends Exception {

public NegativeValueException() {

super("N must be positive.");

public class AverageCalculator {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

try {

System.out.print("Enter the value for N: ");

int N = scanner.nextInt();

if (N < 0) {

throw new NegativeValueException();

int sum = 0;

int count = 0;

for (int i = 0; i < N; i++) {

try {

System.out.print("Enter number #" + (i + 1) + ": ");

int num = scanner.nextInt();


sum += num;

count++;

} catch (InputMismatchException e) {

System.out.println("Invalid input. Please enter an integer.");

scanner.nextLine(); // Clear the input buffer

i--; // Retry entering the number

if (count > 0) {

double average = (double) sum / count;

System.out.println("Average: " + average);

} else {

System.out.println("No numbers entered.");

} catch (NegativeValueException e) {

System.out.println("Exception caught: " + e.getMessage());

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