Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 978551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:04:30+00:00 2026-05-16T04:04:30+00:00

Still struggling with R, especially with error handling: If I use: result <- try(sqlSave(ch,df,tablename=tblTest))

  • 0

Still struggling with R, especially with error handling:

If I use:

result <- try(sqlSave(ch,df,tablename="tblTest"))

I can use:

if (class(result) != "try-error")

to check if something went wrong. No problem.

But if I use try in combination with a function it doesn’t work as I expected:

 result <- try(ch<-odbcConnect("TEST"))

gives “-1” for result and “integer” for class(result)

So should I use

ch<-odbcConnect("TEST")
if (ch != -1)

and use geterrmessage() for the error message?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T04:04:31+00:00Added an answer on May 16, 2026 at 4:04 am

    If you read closely error message you could see that odbcConnect gives you warning. Error is generated by ODBC drivers and it isn’t error in try meaning (geterrmessage() won’t work either).

    You could use tryCatch to handle this, e.g.:

    tryCatch(odbcConnect("TEST"), warning=function(w) print("FAIL!"))
    

    Some more explanation:
    -1 is a result of odbcDriverConnect function. If you look at the code there are lines

    stat <- .Call(C_RODBCDriverConnect, as.character(connection), 
            id, as.integer(believeNRows), as.logical(readOnlyOptimize))
    if (stat < 0L) {
         warning("ODBC connection failed")
         return(stat)
    }
    

    So you end without errors (and with a warning) and with integer code from C-level. Actually this code is returned when connection is succeed too (but then is equal 1). When there is no errors then result class can’t be try-error.
    It is not problem with try and functions but specific of this particular function (odbcDriverConnect).

    You could of course use this behaviour as in your example

    ch <- odbcConnect("TEST")
    if (ch != -1)
    

    With tryCatch you could do

    tryCatch(ch<-odbcConnect("TEST"), warning=function(w) print("FAIL!"))
    

    which creates ch variable when succeed and print message when failed.
    Or

    ch <- tryCatch(odbcConnect("TEST"), warning=function(w) {print("FAIL!");return(NA)})
    

    which always creates ch variable but in case of failure there is NA value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying learn the use of lambda expressions and hence still struggling to
I'm still struggling with changing my Spring Application to use Hibernate with JPA to
I'm still new at jQuery and struggling. Finally, I can get jQuery to toggle
I'm still struggling to find an answer to my question. I want to download
I see there are several posts regarding Cannot set Property and I'm still struggling
I have been struggling with this yesterday whole day, and still couldnt figure it
Still learning magento coding. I wonder is there a way I can print out
Still having issues with this problem. Please help if you can. So I am
I am still struggling with my design and implementation, thought it progresses... First, I
I am still struggling with bitwise operators using Ruby. Getting a value 11100 (28

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.