[ 
https://issues.apache.org/jira/browse/SPARK-13172?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15138423#comment-15138423
 ] 

sachin aggarwal edited comment on SPARK-13172 at 2/9/16 6:33 AM:
-----------------------------------------------------------------

there are two ways we can proceed :- first use printStackTrace and second to 
use mkString()

1) can be ecapsulated in function  :-
def getStackTraceAsString(t: Throwable) = {
    val sw = new StringWriter
    t.printStackTrace(new PrintWriter(sw))
    sw.toString
}

2) println(t.getStackTrace.mkString("\n"))


mkstring approach give extractly same string as old function getStackTraceString

but the output of first approach is more readable.

try this code to see the difference 

{code:title=TrySuccessFailure.scala|borderStyle=solid}
import scala.util.{Try, Success, Failure}
import java.io._

object TrySuccessFailure extends App {

  badAdder(3) match {
    case Success(i) => println(s"success, i = $i")
    case Failure(t) =>
      // this works, but it's not too useful/readable
      println(t.getStackTrace.mkString("\n"))
      println("===================================")
      println(t.getStackTraceString)
      // this works much better
      val sw = new StringWriter
      t.printStackTrace(new PrintWriter(sw))
      println(sw.toString)
  }
  def badAdder(a: Int): Try[Int] = {
    Try({
      val b = a + 1
      if (b == 3) b else {
        val ioe = new IOException("Boom!")
        throw new AlsException("Bummer!", ioe)
      }
    })
  }
  class AlsException(s: String, e: Exception) extends Exception(s: String, e: 
Exception)
}
{code}



was (Author: sachin aggarwal):
there are two ways we can proceed :- first use printStackTrace and second to 
use mkString()

1) can be ecapsulated in function  :-
def getStackTraceAsString(t: Throwable) = {
    val sw = new StringWriter
    t.printStackTrace(new PrintWriter(sw))
    sw.toString
}

2) println(t.getStackTrace.mkString("\n"))


mkstring approach give extractly same string as old function getStackTraceString

but the output of first approach is more readable.

try this code to see the difference 

```
import scala.util.{Try, Success, Failure}
import java.io._

object TrySuccessFailure extends App {

  badAdder(3) match {
    case Success(i) => println(s"success, i = $i")
    case Failure(t) =>
      // this works, but it's not too useful/readable
      println(t.getStackTrace.mkString("\n"))
      println("===================================")
      println(t.getStackTraceString)
      // this works much better
      val sw = new StringWriter
      t.printStackTrace(new PrintWriter(sw))
      println(sw.toString)
  }
  def badAdder(a: Int): Try[Int] = {
    Try({
      val b = a + 1
      if (b == 3) b else {
        val ioe = new IOException("Boom!")
        throw new AlsException("Bummer!", ioe)
      }
    })
  }
  class AlsException(s: String, e: Exception) extends Exception(s: String, e: 
Exception)
}
```


> Stop using RichException.getStackTrace it is deprecated
> -------------------------------------------------------
>
>                 Key: SPARK-13172
>                 URL: https://issues.apache.org/jira/browse/SPARK-13172
>             Project: Spark
>          Issue Type: Sub-task
>          Components: Spark Core
>            Reporter: holdenk
>            Priority: Trivial
>
> Throwable getStackTrace is the recommended alternative.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@spark.apache.org
For additional commands, e-mail: issues-h...@spark.apache.org

Reply via email to