Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Throwing exceptions or any other alternatives?

 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm writing a piece of scala code that will read a set of values from a configuration file. The values are read to an Option[String]. Now my question here is, I do a match case and get the value. Suppose if the value is not there, it would match the case None and in that scenario what should I do? I do not want to follow the old practice of throwing an exception like I have been used to doing in java. The important criteria here is that if I land in case None, I cannot proceed with my application. Imagine that I'm reading the db connection credentials here, so without the db connection credentials, my application cannot do anything. At the same time I also do not want to throw an exception to the caller. Is there a cleverer way to do this in Scala?
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So here is the simple code that I was talking about!



How could the case None be elegantly handled?
 
chris webster
Bartender
Posts: 2407
33
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I'm guessing you could return an Option on your dbhost, but that still leaves you checking for a None and then deciding what to do with it. Or you could do some super-clever reactive stuff with Futures and so on, although that seems kind of over-complex for a simple yes-or-no function. Maybe you want to wrap the whole connect-to-the-database thing in a function that returns an Option (or whatever) and simply branch on that once you've either returned your connection or a None.

But at some point you still have to decide either to throw an exception, or display some other kind of message to your user telling them they're not getting into the database today.
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chris webster wrote:Well, I'm guessing you could return an Option on your dbhost, but that still leaves you checking for a None and then deciding what to do with it. Or you could do some super-clever reactive stuff with Futures and so on, although that seems kind of over-complex for a simple yes-or-no function. Maybe you want to wrap the whole connect-to-the-database thing in a function that returns an Option (or whatever) and simply branch on that once you've either returned your connection or a None.

But at some point you still have to decide either to throw an exception, or display some other kind of message to your user telling them they're not getting into the database today.


I'm not that familiar with the Futures concept. Not sure how that would help me here. If any one of those values are not present, the user cannot do anything. I have to throw an exception to the caller to show the user some sore of message. Anyways, here is another version that I'm thinking of:



// Check if checkAndPrepareDBConfig tuple is a Some or None. If it is a Some, I know all the values were available but if None, something has gone wrong and I have to react. But how do I know from the above code snippet which field was not present?
 
Sresh Rangi
Ranch Hand
Posts: 52
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Harry wrote:


// Check if checkAndPrepareDBConfig tuple is a Some or None. If it is a Some, I know all the values were available but if None, something has gone wrong and I have to react. But how do I know from the above code snippet which field was not present?


You can use Either instead of Option. Either is like an option, but can contain an error message if its value is missing.
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sresh Rangi wrote:
Joe Harry wrote:


// Check if checkAndPrepareDBConfig tuple is a Some or None. If it is a Some, I know all the values were available but if None, something has gone wrong and I have to react. But how do I know from the above code snippet which field was not present?


You can use Either instead of Option. Either is like an option, but can contain an error message if its value is missing.


I was in fact considering using Either to propagate the errors to my Play controllers rather than dealing with throwing Exceptions. My method signatures have Either[Failure, A] as return types where A will depend on what the method will return. But I'm not sure how this would fit into my scenario above where-in I combine multiple calls and return a tuple.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic