Menu
Question Index
...

Handle the exceptions such that the program compiles and prints the output as shown below.

Input (Integer) Printed Output
1 Exception A occured with error code = 5
2 Exception B occured with difference = 1.23
3 Exception C occured with reason = Invalid Data



class HandleExceptions
{
    public static void main(String s[])
    {
        process(1);

    }


    public static void process(int input) {
    try {
    method(input);
    }
    }
    
    public static void method(int input) throws AEx, BEx, CEx {
    if (input == 1) {
    throw new AEx(5);
    }
    if (input == 2) {
    throw new BEx(1.23);
    }
    if (input == 3) {
    throw new CEx("Invalid Data");
    }
    }

}
class AEx extends Exception {

int errorCode;

AEx(int errorCode) {
this.errorCode = errorCode;
}
}

class BEx extends Exception {

double difference;

BEx(double difference) {
this.difference = difference;
}
}

class CEx extends Exception {

String reason;

CEx(String reason) {
this.reason = reason;
}
}

Doubts

Problems

Topic: Exception Handling Syntax In Java Programming

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App