Posts

Showing posts with the label optional

Android Studio Requires Catching Throwable when using orElseThrow method

Android Studio Requires Catching Throwable when using orElseThrow method I am writing an application in Android Studio. My current version is 3.1.0. I am trying to use the orElseThrow method from the Optional class. I checked the settings for Android Studio, and it requires an API level of at least 24 to use Optional. I'm currently using API level 25, and my JDK is 1.8.0_151. I have created a new Android Studio project using an Empty Activity. I have created a class called TestClass that contains the following code: package com.examples.myapplication; import java.util.Arrays; import java.util.List; public final class TestClass { private final List<Integer> numbers; public TestClass() { numbers = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); } public final int doesNumberExist(final int number) { return numbers.stream().filter(n -> n == number).findAny().orElseThrow(RuntimeException::new); } } The problem that I'm having here is that An...