2

I am implementing simple guava CacheLoader as it is stated here

   CacheLoader<Key, Graph> loader = new CacheLoader<Key, Graph>() {
     public Graph load(Key key) throws AnyException {
       return createExpensiveGraph(key);
     }
   };
   LoadingCache<Key, Graph> cache = CacheBuilder.newBuilder().build(loader);

Does this mean that I should create my own exception class and throw it if the value is null or is there any better way to hande the situation?

lopushen
  • 1,117
  • 2
  • 11
  • 32

1 Answers1

4

You're allowed to throw whatever exception you want -- that can be something you made, or something built-in. There's no special rule for what exception is most appropriate for caches -- just use whatever exception would be most appropriate otherwise.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • what would happen to the cache value when exception occurs for that key? Is it propagated to caller of `cache.get(key)`? What if I want to gracefully Ignore that exception? Does returning `null` makes sense in that case? – Arun Gowda Sep 09 '20 at 13:35