diff --git a/src/main/java/net/lostluma/battery/api/Battery.java b/src/main/java/net/lostluma/battery/api/Battery.java index 8a9403d..7e6b225 100644 --- a/src/main/java/net/lostluma/battery/api/Battery.java +++ b/src/main/java/net/lostluma/battery/api/Battery.java @@ -1,5 +1,6 @@ package net.lostluma.battery.api; +import net.lostluma.battery.api.exception.InvalidStateError; import org.jetbrains.annotations.NotNull; import java.io.IOException; @@ -127,7 +128,7 @@ public interface Battery { * Refresh battery information in-place. * * @throws IOException battery information couldn't be refreshed. - * @throws RuntimeException the associated manager is not active. + * @throws InvalidStateError the associated manager isn't active. */ - void update() throws IOException, RuntimeException; + void update() throws IOException, InvalidStateError; } diff --git a/src/main/java/net/lostluma/battery/api/Manager.java b/src/main/java/net/lostluma/battery/api/Manager.java index b4f1e89..0e3e510 100644 --- a/src/main/java/net/lostluma/battery/api/Manager.java +++ b/src/main/java/net/lostluma/battery/api/Manager.java @@ -1,5 +1,6 @@ package net.lostluma.battery.api; +import net.lostluma.battery.api.exception.InvalidStateError; import net.lostluma.battery.api.exception.LibraryLoadError; import net.lostluma.battery.impl.ManagerImpl; import org.jetbrains.annotations.NotNull; @@ -32,9 +33,9 @@ public interface Manager extends AutoCloseable { * * @return the system's current batteries. * @throws IOException looking up the batteries failed. - * @throws RuntimeException the manager is already closed. + * @throws InvalidStateError the manager is already closed. */ - @NotNull Collection batteries() throws IOException, RuntimeException; + @NotNull Collection batteries() throws IOException, InvalidStateError; /** * Close the manager. diff --git a/src/main/java/net/lostluma/battery/api/exception/InvalidStateError.java b/src/main/java/net/lostluma/battery/api/exception/InvalidStateError.java new file mode 100644 index 0000000..db9f3b0 --- /dev/null +++ b/src/main/java/net/lostluma/battery/api/exception/InvalidStateError.java @@ -0,0 +1,14 @@ +package net.lostluma.battery.api.exception; + +import org.jetbrains.annotations.ApiStatus; + +/** + * Error thrown when calling a method on a closed object. + */ +// TODO (breaking): Subclass Error instead +public class InvalidStateError extends RuntimeException { + @ApiStatus.Internal + public InvalidStateError(String message) { + super(message); + } +} diff --git a/src/main/java/net/lostluma/battery/impl/BatteryImpl.java b/src/main/java/net/lostluma/battery/impl/BatteryImpl.java index 86a5e01..49edab1 100644 --- a/src/main/java/net/lostluma/battery/impl/BatteryImpl.java +++ b/src/main/java/net/lostluma/battery/impl/BatteryImpl.java @@ -1,5 +1,6 @@ package net.lostluma.battery.impl; +import net.lostluma.battery.api.exception.InvalidStateError; import net.lostluma.battery.api.Battery; import net.lostluma.battery.api.State; import net.lostluma.battery.api.Technology; @@ -49,11 +50,11 @@ private BatteryImpl(long ptr, ManagerImpl manager, Technology technology, @Nulla } @Override - public void update() throws IOException, RuntimeException { + public void update() throws IOException, InvalidStateError { if (this.manager.isActive()) { this.update0(); } else { - throw new RuntimeException("Attached manager is closed."); + throw new InvalidStateError("Attached manager is closed."); } } diff --git a/src/main/java/net/lostluma/battery/impl/ManagerImpl.java b/src/main/java/net/lostluma/battery/impl/ManagerImpl.java index d5f5c48..4637050 100644 --- a/src/main/java/net/lostluma/battery/impl/ManagerImpl.java +++ b/src/main/java/net/lostluma/battery/impl/ManagerImpl.java @@ -2,6 +2,7 @@ import net.lostluma.battery.api.Battery; import net.lostluma.battery.api.Manager; +import net.lostluma.battery.api.exception.InvalidStateError; import net.lostluma.battery.api.exception.LibraryLoadError; import net.lostluma.battery.impl.util.NativeUtil; import org.jetbrains.annotations.ApiStatus; @@ -41,7 +42,7 @@ boolean isActive() { } @Override - public @NotNull Collection batteries() throws IOException, RuntimeException { + public @NotNull Collection batteries() throws IOException, InvalidStateError { if (this.isActive()) { BatteryImpl[] batteries = this.batteries0(); @@ -57,7 +58,7 @@ boolean isActive() { return Arrays.asList(batteries); } else { - throw new RuntimeException("Manager can not be used after being closed!"); + throw new InvalidStateError("Manager can not be used after being closed!"); } }