Quico Llinares Llorens Quico Llinares Llorens - 1 year ago 68
Java Question

Is it possible to call a method in catch block?

Hi I am doing a web service, and when I create a Socket I need to know if it is created, if it is not I have to show an error webpage, and I use the method sendResponse to know what to show in the web browser.

This is the part of my code that has the error:

try {
Socket controller = new Socket (ipCon, portCon);
writeSocket (resourceName, controller);
String aux2 = readSocket(controller);
System.out.println("Hemos recibido del controlador: " + aux2);
if (aux2.indexOf("ERROR") == -1) {
sendResponse (200, aux2, false);
} else {
sendResponse (404, "error.html", true);

} catch (Exception e) {
sendResponse(409, "409.html",true);

The error that I get when compiling is the next:
error: unreported exception Exception; must be caught or declared to be thrown

Thanks for your help! If you need some more info let me know.

Answer Source

It's definitely possible but the Exception thrown within the catch block is not caught by the same catch block. Therefore you have to wrap into another try-catch:

    } catch (Exception e) {
        try {
            sendResponse(409, "409.html",true);
        } catch (Exception ex) {
            // do some error handling for sendResponse()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download