user2879704
user2879704

Reputation:

java's e.printStackTrace equivalent in haskell

I am trying out haskell's kafka library from git and got this error. To debug this error, i like to print stacktrace at the error line.

In python world, it is just,

import traceback; print traceback.print_exc()

(or) in java, it is

e.printStackTrace()

So, how to do the same in haskell world?

Upvotes: 3

Views: 262

Answers (1)

Arbus
Arbus

Reputation: 304

You can get stack traces in Haskell but it is not as convenient as just e.printStackTrace(). Here is a minimal example:

import Control.Exception
import Debug.Trace

getStack :: String -> SomeException -> IO a
getStack msg e = traceStack (show e) $ error msg

main :: IO ()
main = do
    (head []) `catch` (getStack "error on main at head")

Finally, compile it with ghc -prof -fprof-auto StackTrace.hs and it will produce

Prelude.head: empty list              
Stack trace:                          
  Main.getStack (StackTrace.hs:5:9-56)     
  Main.main (StackTrace.hs:(8,9)-(9,74))   
  GHC.List.CAF (<entire-module>)      
StackTrace.exe: error on main at head

Upvotes: 3

Related Questions