File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed
Section18ExceptionHandling/src Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments