简体   繁体   中英

Android studio try-catch suggestion for my method

I build my own library in Android and i want to give a warning about try-catch block when my method use

For example my library class like below

public class MyLib{
    public void foo(){
        // any code here
    }
}

and the user's activity

MyLib lib = new MyLib();
lib.foo();

i want to here give a warning and after when the user press the Alt + Enter like below

MyLib lib = new MyLib();

try{
    lib.foo();
}catch(Exception e){
}

is there code, annotation or configration about this?

When the IDE warns you about the unhandled exception, it means that the method throws a checked exception. This is what it should look like if your method throws an IOException :

public void foo() throws IOException {
}

Read more about checked exceptions here .

Assume your method throws JSONException , add it to your method signature

public class MyLib{
    public void foo() throws JSONException{
        // any code here
    }
}

Now whichever class would try to access your code has to catch or throw this 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