Saturday, April 22, 2023

R Tutorial Playlist - Error handler, try catch statement

Try catch statement in R is function that is going to help you to preserve process flow even if something unexpected like error happens. So, even if your execution runs on error, your code is not going to crash. 

tryCatch(

    {

        "17" / 8

    },

    warning= function(w){

        print(w)

    },

    error= function(e){

        print(e)

    },

    finally={

        print("anyway")

    }

)

First argument of this function is, actual, problematic code, or code that could produce an error. With warning argument, we are able to provide response on warnings where argument w is actual textual representation of warning itself. Unlike errors, warnings are not going to crash your code.

Argument error provides function that is going to be executed if error happens, where e is actual textual representation of error itself.

And statement provided with argument finally is going to be executed anyway.


Check video link below.



No comments:

Post a Comment