0% found this document useful (0 votes)
16 views4 pages

Exception handling-ChiranjeebLAB

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)
16 views4 pages

Exception handling-ChiranjeebLAB

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
You are on page 1/ 4

School: ............................................................................................................. Campus: .......................................................

Academic Year: ...................... Subject Name: ........................................................... Subject Code: ..........................

Semester: ............... Program: ........................................ Branch: ......................... Specialization: ..........................

Date: .....................................

(Learning by Doing and Discovery)

* Coding Phase: Pseudo Code / Flow Chart / Algorithm


1. Write a Java program that reads an integer from the user and calculates the
factorial of that number. Handle the scenario where the user enters a
negative number by throwing a custom NegativeNumberException and
catching it to display an error message. Ensure that your program uses
exception handling to handle this case.

* As applicable according to the experiment.


Two sheets per experiment (10-20) to be used.
Applied and Action Learning

* Testing Phase: Compilation of Code (error detection)

* As applicable according to the experiment.


Two sheets per experiment (10-20) to be used.
Applied and Action Learning

* Implementation Phase: Final Output (no error)

import java.util.Scanner;

class NegativeNumberException extends Exception {


public NegativeNumberException(String message) {
super(message);
}
}

public class FactorialCalculator {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a non-negative integer to calculate its
factorial: ");
int number = scanner.nextInt();

try {
if (number < 0) {
throw new NegativeNumberException("Negative number
entered.");
}

long factorial = calculateFactorial(number);


System.out.println("Factorial of " + number + " is: " + factorial);
} catch (NegativeNumberException e) {
System.out.println("Error: " + e.getMessage());
}
}

private static long calculateFactorial(int number) {


if (number == 0) {
return 1;
}
return number * calculateFactorial(number - 1);
}
}

* As applicable according to the experiment.


Two sheets per experiment (10-20) to be used.
Applied and Action Learning

Rubrics
Concept 10
Planning and Execution/ 10
Practical Simulation/ Programming
Result and Interpretation 10
Record of Applied and Action Learning 10
Viva 10
Total 50

Signature of the Student:

Signature of the Faculty:


* As applicable according to the experiment.
Two sheets per experiment (10-20) to be used.

You might also like