-2

I need to see the result as a boolean result: true. But there's a catch I need to do it in a non-ordinary way.

import java.io.IOException;

public class FlashLight {

    private Bulb bulb;
    private Battery[] batteries;

    public void on() {
        try {

            if (this.IsThereEnoughPower()) {

                this.bulb.setOn(true);

                for (Battery b : batteries) {
                    b.setPower(b.getPower() - this.bulb.getBrightness());
                }
            }

        } catch (IOException e) {
            System.out.println(e.getMessage());

            this.setBatteries(new Battery[4]);

        }
    }

I need to catch the exception in method on() but i can only modify method: DetermineIfFlashlightCanBeTurnedOn

    public boolean DetermineIfFlashlightCanBeTurnedOn() throws IOException {

        return bulb != null && DetermineIfBatteriesAreInstalled() && IsThereEnoughPower();
    }

    private boolean DetermineIfBatteriesAreInstalled() throws IOException {
        if (batteries.length < 4) {
            throw new IOException(Math.abs(-4 + batteries.length));
        }
        for (Battery b : batteries) {
            if (b == null) {
                return false;
            }
        }

        return true;
    }

    private boolean IsThereEnoughPower() {
        for (Battery b : batteries) {
            if (b.getPower() < MIN_BATTERY_POWER) {
                return false;
            }
        }

        return true;
    }

    private static void testLatarki(String... args) {

        FlashLight flashlight = new Flashlight();
        System.out.println(flashlight.DetermineIfFlashlightCanBeTurnedOn());
    }
}

Exception can be caught only in on() method. DetermineIfBatteriesAreInstalled() DetermineIfFlashlightCanBeTurnedOn must be signed as: throws IOException.

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140

1 Answers1

2

You can use try{}catch(){} instead :

public boolean DetermineIfFlashlightCanBeTurnedOn() {
    try {
        return bulb != null && DetermineIfBatteriesAreInstalled() && IsThereEnoughPower();
    } catch (Exception e) {
        //log your exception
    }
    return false;
}

I forgot to tell you guys i can use try/catch blocks only in on() method


In this case you can use RuntimeException you don't need to use throws IOException in your method:

if (batteries.length < 4) {
    throw new RuntimeException(Math.abs(-4 + batteries.length)+"");
}

So :

public boolean DetermineIfFlashlightCanBeTurnedOn() {
//--not need to use throw throws IOException-------^ 
    return bulb != null && DetermineIfBatteriesAreInstalled() && IsThereEnoughPower();
}

private boolean DetermineIfBatteriesAreInstalled() {
//--not need to use throw throws IOException------^ 
    if (batteries.length < 4) {
        throw new RuntimeException(Math.abs(-4 + batteries.length) + "");
        //----------^^ 
    }
    for (Battery b : batteries) {
        if (b == null) {
            return false;
        }
    }

    return true;
}

You can read more here Is there a way to throw an exception without adding the throws declaration?

Community
  • 1
  • 1
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • I forgot to tell you guys i can use try/catch blocks only in on() method – SpicyJam May 13 '17 at 09:30
  • I'm sorry, but it is unclear for me what your code does and what you are asking, even with your edit. I'm under the impression that you are not writing Java code, but Java-ized Objective-C or C++. – M. le Rutte May 13 '17 at 10:39
  • @M.leRutte the OP want to throw and catch the exception without using throw Exception in methods and use only try{}catch in on() method for that i suggest to use `RuntimeException` which not required to use throw Exception in method i hope this is what the OP need – Youcef LAIDANI May 13 '17 at 11:02