Ali Zali
Ali Zali

Reputation: 393

" must be caught or declared to be thrown" in react native when using NativeModules

I want to build a native module to react-native that reboot device. I find this Turn off device programmatically in java. It works alone in android studio but when I try to use it in a native module I got the bellow error

https://pasteboard.co/IztJ6ll.jpg

C:\Users\Ali\Desktop\Android\ReactNative\ReactNativeTest\android\app\src\main\ja
va\com\reactnativetest\ToastModule.java:42: error: unreported exception IOExcept
ion; must be caught or declared to be thrown
    Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", "reboot"
 });
                                            ^
C:\Users\Ali\Desktop\Android\ReactNative\ReactNativeTest\android\app\src\main\ja
va\com\reactnativetest\ToastModule.java:43: error: unreported exception Interrup
tedException; must be caught or declared to be thrown
      proc.waitFor();
                  ^
2 errors

> Task :app:compileDebugJavaWithJavac FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug
option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 9s
57 actionable tasks: 1 executed, 56 up-to-date
error Could not install the app on the device, read the error above for details.

Make sure you have an Android emulator running or a device connected and have
set up your Android development environment:
https://facebook.github.io/react-native/docs/getting-started.html
error Command failed: gradlew.bat app:installDebug. Run CLI with --verbose flag
for more details.

this is my native module:

package com.reactnativetest;
import android.widget.Toast;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import java.util.Map;
import java.util.HashMap;
import java.util.*;
import java.io.*;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;


public class ToastModule extends ReactContextBaseJavaModule {
  private static final String DURATION_SHORT_KEY = "SHORT";
  private static final String DURATION_LONG_KEY = "LONG";
  public ToastModule(ReactApplicationContext reactContext) {
    super(reactContext);
  }

@Override
  public String getName() {
    return "ToastExample";
  }

  @Override
  public Map<String, Object> getConstants() {
    final Map<String, Object> constants = new HashMap<>();
    constants.put(DURATION_SHORT_KEY, Toast.LENGTH_SHORT);
    constants.put(DURATION_LONG_KEY, Toast.LENGTH_LONG);
    return constants;
  }

  @ReactMethod
  public void show(String message, int duration) {
    Toast.makeText(getReactApplicationContext(), message, duration).show();
    Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", "reboot" });
      proc.waitFor();
  }
}

Upvotes: 3

Views: 377

Answers (1)

Nir Levy
Nir Levy

Reputation: 12953

it is right there in the logs:

C:\Users\Ali\Desktop\Android\ReactNative\ReactNativeTest\android\app\src\main\ja va\com\reactnativetest\ToastModule.java:43: error: unreported exception InterruptedException; must be caught or declared to be thrown proc.waitFor();

proc.waitFor() throws an exception. You should either surround it with try-catch, or declare in the method signature throws InterruptedException and handle it elsewhere

Upvotes: 1

Related Questions