KUNJESH KUMAR MISHRA
KUNJESH KUMAR MISHRA

Reputation: 41

How to catch com.microsoft.sqlserver.jdbc.SQLServerException: The query has timed out

I am having some of the stored procedures in the MS-SQL and I wanted to do certain piece of codes in case of timeout issue. I have done a few things which I want to share:

catch (Exception e) {
boolean bool = e.getClass().equals(SQLServerException.class);
  if(bool){
  //-- My piece of code logic
 }
}

My problem is, I want to do my piece of code in case of only

    com.microsoft.sqlserver.jdbc.SQLServerException: The query has timed out

How to catch that particular timeout exception?

Upvotes: 1

Views: 4126

Answers (2)

KUNJESH KUMAR MISHRA
KUNJESH KUMAR MISHRA

Reputation: 41

Just got a solution which solved my problem:

catch(Exception e){
 boolean bool = e.getMessage().contains("The query has timed out");
 if(bool){
   //-- My piece of code logic
  }
}

Upvotes: 2

e.g78
e.g78

Reputation: 697

You can use case to check the error code in the exception.

catch (SQLException se) {
        switch (se.getErrorCode()) {
        case 1:  doSomething();
            break;
        case 2:  doSomethingElse();
            break;
    }
}

The list of the error codes is here https://learn.microsoft.com/en-us/azure/sql-database/sql-database-develop-error-messages

Upvotes: 0

Related Questions