0

So let's say I'm converting a large collection of data from a server into custom local java objects. The POJO has an int variable, which is what I expect to get from the server. Only, let's say some of the data lists the number as a string instead of an integer. I have a for loop set up like:

for (Object document : DataSentFromServer) {                                   
   MyObjectClassArrayList.add(document.toObject(MyObject.class));
}

So for 99% of the documents have the int as an int, but one has it as a String. Thus when the for loop reaches that document it throws the java.lang.RuntimeException: Could not deserialize object. Failed to convert a value of type java.lang.String to int I know that I need to update the data on the server, I already did this to solve the issue.

My question is: How can I create a catch block or something that will simply skip over documents from the server that don't match the data model of my object class? As I don't want my client side apps crashing if something is wrong with server data.

Mr.Drew
  • 939
  • 2
  • 9
  • 30
  • Catching runtime exceptions is not very good idea except if you know exactly what you are doing. May be better way would be to adopt deffensive programming and check the data type before adding to the list. But using `instance of` is not good idea too. – Evgeni Enchev Jan 31 '19 at 07:21
  • Why isn't it a good idea? Does that cause performance or memory leak issuses in Java? And which one is more computationally efficient? Iterating over each variable in all the data and checking it's type before converting it to the local object, OR simply catching a runtime error ? – Mr.Drew Jan 31 '19 at 14:06
  • In your case I'd do the same - catch the runtime exception, just for simplicity. But take a look at some basic java book and you'll see *why* cathcing runtime exceptions is not good idea. More over - this should be exceptional situation that should never happen, so no need to catch it. What you need is receive correct data set. – Evgeni Enchev Jan 31 '19 at 14:20

2 Answers2

2

Simple surround the function with try catch block:

for (Object document : DataSentFromServer) {  
    try{                                 
       MyObjectClassArrayList.add(document.toObject(MyObject.class));
    }catch(RuntimeException e){
      //do something with the bad data if you wish.
    }
}
Maksim Novikov
  • 835
  • 6
  • 18
  • Thanks, so will a RuntimeException handle any code block that can cause the program to crash while executing? (Java Android) – Mr.Drew Jan 31 '19 at 15:29
  • @Mr.Drew it will handle all `RuntimeException`'s that will be thrown during `MyObjectClassArrayList.add(document.toObject(MyObject.class));` operation, if you see that another Exception thrown in this process you can add another catch block or add like this : `catch(RuntimeException | IllegalArgumentException e){` to handle the exception in one block. – Maksim Novikov Jan 31 '19 at 15:34
  • Yes, I understand that. I suppose I mean to ask if all Java Runtime Exceptions cause Android apps to crash? – Mr.Drew Feb 03 '19 at 04:22
1

A little update on @Marksim Novikov answer.

 for (Object document : DataSentFromServer) {  
try{                                 
   MyObjectClassArrayList.add(document.toObject(MyObject.class));
}catch(RuntimeException e){
  //continue will skip the current iteration and go to next iteration
   continue;
  }
}

So, if you run into Runtime exception, it will skip that iteration and go to next one.

Ron Daulagupu
  • 423
  • 6
  • 18