简体   繁体   中英

How to make android onCreate throw exceptions

I am using Jackson on android to parse a JSON file into an object, and the object mapper (or object mapper.readvalue, I am not sure) throws an IOException .

So I added throws IOException to my method.

public TextBox JsonToTextBox () throws IOException {
}

My problem is that I am calling that method from onCreate , which doesn't let me throw an exceptions. Any ideas?

Image which explains why I cant throw exceptions

I know, I can use a try catch, I was just looking how to do it by throwing an exception from onCreate, thanks for the answers though

If you cannot handle the exception properly, you can "convert" it into a RuntimeException and re-throw:

@Override
protected void onCreate(Bundle savedInstanceState) {
    //...
    try {
        TextBox textBox = JsonToTextBox();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
} 

Otherwise you can catch the exception when using your method with a try catch block.

@Override
protected void onCreate(Bundle savedInstanceState) {
    //...
    try {
        TextBox textBox = JsonToTextBox();
    } catch (IOException) {
        //Handle exception
    }
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM