hmm, exceptions...

40 views
Skip to first unread message

Vlad Patryshev

unread,
May 20, 2013, 9:02:32 PM5/20/13
to scala-user
Seems like scala people use to look down at exceptions... but see. The difference between an error message and an exception is that an exception tells you where the error happened. I actually use it in pretty unusual places, like this:

def jsREPL(implicit prompt:String = "type your js") {
  val here = Thread.currentThread.getStackTrace()(3)
  println(s"$prompt [${here.getFileName}:${here.getLineNumber}]: ")
  Source.fromInputStream(System.in).getLines.takeWhile(!_.isEmpty) foreach {
    s => println(tryOr(runJS(s), (_:Exception).getMessage))
  }


What I want to say: if we, in Scala, had a habit of passing around exceptions, instead of plain text (in Failure or Left), it would be pretty helpful. 

What do you think?

Thanks,
-Vlad

Jed Wesley-Smith

unread,
May 20, 2013, 9:10:53 PM5/20/13
to Vlad Patryshev, scala-user
There is nothing wrong with passing back a Throwable as the left value. The problem is in _throwing_ the exception. Throwing exceptions is a side effect (in that it cannot be replaced by a value), and that is why it is "looked down upon".


--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Maurício Linhares

unread,
May 20, 2013, 9:23:55 PM5/20/13
to scala-user
With Try in 2.10 I think you don't have to use Either for handling
exceptions. Either is much more useful in representing something that
can be in two different states, like validation. You could have the
valid object being returned in a Right if it is ok or giving back a
collection of validation errors in the Left if it is not

And with Try, Failure holds an exception, so there is no need to send
plain text, just hand the exception to the Failure and you're done.
-
Maurício Linhares
http://mauricio.github.io/ - http://twitter.com/#!/mauriciojr

Ryan LeCompte

unread,
May 20, 2013, 9:31:50 PM5/20/13
to Maurício Linhares, scala-user
Try, Either, Option, scala.util.control.Exception.allCatch, oh my!

Oliver Ruebenacker

unread,
May 20, 2013, 10:17:20 PM5/20/13
to Jed Wesley-Smith, Vlad Patryshev, scala-user

     Hello,

On Mon, May 20, 2013 at 9:10 PM, Jed Wesley-Smith <j...@wesleysmith.io> wrote:
There is nothing wrong with passing back a Throwable as the left value. The problem is in _throwing_ the exception. Throwing exceptions is a side effect (in that it cannot be replaced by a value), and that is why it is "looked down upon".

  Why is this a side effect? An exception is a value.

     Take care
     Oliver


--
Head of Systems Biology Task Force at PanGenX (http://www.pangenx.com)
Any sufficiently advanced technology is indistinguishable from magic.

Naftoli Gugenheim

unread,
May 20, 2013, 10:58:31 PM5/20/13
to Oliver Ruebenacker, Jed Wesley-Smith, Vlad Patryshev, scala-user
On Mon, May 20, 2013 at 10:17 PM, Oliver Ruebenacker <cur...@gmail.com> wrote:

     Hello,

On Mon, May 20, 2013 at 9:10 PM, Jed Wesley-Smith <j...@wesleysmith.io> wrote:
There is nothing wrong with passing back a Throwable as the left value. The problem is in _throwing_ the exception. Throwing exceptions is a side effect (in that it cannot be replaced by a value), and that is why it is "looked down upon".

  Why is this a side effect? An exception is a value.

Because it affects control flow.
 

     Take care
     Oliver


--
Head of Systems Biology Task Force at PanGenX (http://www.pangenx.com)
Any sufficiently advanced technology is indistinguishable from magic.

--

Vlad Patryshev

unread,
May 21, 2013, 12:32:17 AM5/21/13
to Oliver Ruebenacker, Jed Wesley-Smith, scala-user
Well, I'd say we are outside of the regular set-theoretic functions, and somewhere in Kleisli category for a certain monad, that's why.

Thanks,
-Vlad

Roland Kuhn

unread,
May 21, 2013, 2:40:25 AM5/21/13
to Jed Wesley-Smith, Vlad Patryshev, scala-user
A minor but possibly important nitpick: throwing the exception is fine, it means non-termination or “bottom”. Catching the exception is not fine and a side-effect.

A thought experiment: if not caught, the exception will terminate the thread and thus all functions which were called do not terminate, nobody observes anything out of the ordinary. If caught, one function forces some others (above it on the call stack) to return something they had no control over and which is actually determined by none of them but by the code which caught the exception.

I said “minor” above because in usual programs most exceptions will end up being caught, thus throwing them includes the later side-effect.

[disclaimer: I have not studied CS, take this as a physicists view on the matter]

Regards,

Roland


Dr. Roland Kuhn
Akka Tech Lead
Typesafe – Empowering professional developers to build amazing apps.
twitter: @rolandkuhn

See you at Scala Days 2013 in NYC!
June 10th - June 12th
www.scaladays.org

Reply all
Reply to author
Forward
0 new messages