wood burning stoves 2.0*
The moose likes Scala and the fly likes Throwing exceptions or any other alternatives? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » Scala
Bookmark "Throwing exceptions or any other alternatives?" Watch "Throwing exceptions or any other alternatives?" New topic
Author

Throwing exceptions or any other alternatives?

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

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?


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

So here is the simple code that I was talking about!



How could the case None be elegantly handled?
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1614
    
  13

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.


No more Blub for me, thank you, Vicar.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

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

Joined: Nov 28, 2012
Posts: 45
    
    1
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

Joined: Sep 26, 2006
Posts: 9345
    
    2

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Throwing exceptions or any other alternatives?
 
Similar Threads
SecurityContextPersistenceFilter where to configure it and how spring will use it
Identifying a new physical connection versus a logical connection in a jdbc connection pool
Starting OCMJD
Handling Exception in ServletContextListener
Exceptions