Skip to content

Commit 6a8b741

Browse files
committed
Added Tips and Tricks.
Signed-off-by: Someshdiwan <[email protected]>
1 parent 7c897f0 commit 6a8b741

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Submit to check the output
2+
import java.util.*;
3+
4+
class Rectangle {
5+
public static int calculateArea(int length, int width) throws NegativeValueException {
6+
if (length < 0 || width < 0) {
7+
throw new NegativeValueException("Error: length or width cannot be negative");
8+
}
9+
return length * width;
10+
}
11+
}
12+
13+
class NegativeValueException extends Exception {
14+
public NegativeValueException(String message) {
15+
super(message);
16+
}
17+
}
18+
19+
class CustomExceptions
20+
{
21+
public static void main (String[] args)
22+
{
23+
try {
24+
int length = -5;
25+
int width = 3;
26+
27+
Rectangle rectangle = new Rectangle();
28+
int area = rectangle.calculateArea(length, width);
29+
System.out.println("Area: " + area);
30+
}
31+
catch (NegativeValueException e) {
32+
System.out.println(e.getMessage());
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)