2017-03-22 6 views
2

私は以下を試して、うまくいった。ghciで無効な整数を読む

ghci> read "1232" :: Int 
1232 

もちろん、以下は無効ですが、エラーメッセージは直感的ではありません。

ghci> read "12 32" :: Int 
Stopped in <exception thrown>, <unknown> 
_exception :: e = _ 
Unable to list source for <unknown> 
Try rerunning with :trace, :back then :list 
ghci> _exception 

<interactive>:21:1: error: 
    ? No instance for (Show e) arising from a use of ‘print’ 
     Cannot resolve unknown runtime type ‘e’ 
     Use :print or :force to determine these types 
     Relevant bindings include it :: e (bound at <interactive>:21:1) 
     These potential instances exist: 
     instance (Show b, Show a) => Show (Either a b) 
      -- Defined in ‘Data.Either’ 
     instance Show Ordering -- Defined in ‘GHC.Show’ 
     instance Show Integer -- Defined in ‘GHC.Show’ 
     ...plus 23 others 
     ...plus 28 instances involving out-of-scope types 
     (use -fprint-potential-instances to see them all) 
    ? In a stmt of an interactive GHCi command: print it 

実行時エラーのような結果が期待されていましたが、どのようにしてこの問題が発生するのか分かりません。この例外に対するロジックはありますか?

+7

この欠落しているインスタンスは、実際には例外です。あなたが例外を有効にしてブレークした場合、それを無効にすると 'Prelude.read:no parse'が得られます – luqui

答えて

1

debugにプログラムを追加しない限り、-fbreak-on-exception or -fbreak-on-errorでGHCiを実行しないでください。あなたが-fbreak-on-exceptionを使用する場合、例外が再スローを取得する傾向があるので、単一continueが、十分ではないかもしれない、とあなたはそれぞれの例外を処理する必要があること

$ /opt/ghc/8.0.2/bin/ghci -fbreak-on-error                                  
GHCi, version 8.0.2: http://www.haskell.org/ghc/ :? for help 

Prelude> read "12 32" :: Int 
Stopped in <exception thrown>, <unknown> 
_exception :: e = _ 

[<unknown>] Prelude> :continue 
*** Exception: Prelude.read: no parse 

注:

$ /opt/ghc/8.0.2/bin/ghci -fbreak-on-exception                                 
GHCi, version 8.0.2: http://www.haskell.org/ghc/ :? for help 

Prelude> read "12 32" :: Int 
Stopped in <exception thrown>, <unknown> 
_exception :: e = _ 

[<unknown>] Prelude> :continue 
Stopped in <exception thrown>, <unknown> 
_exception :: e = _ 

[<unknown>] Prelude> :continue 
Stopped in <exception thrown>, <unknown> 
_exception :: e = _ 

[<unknown>] Prelude> :continue 
*** Exception: Prelude.read: no parse 
このケースでは、 :continueを続行することができますnはまた

$ /opt/ghc/8.0.2/bin/ghci -fno-break-on-exception -fno-break-on-error                                 
GHCi, version 8.0.2: http://www.haskell.org/ghc/ :? for help 

Prelude> read "12 32" :: Int 
*** Exception: Prelude.read: no parse 

break-on-Xせずに、我々はすぐに示した例外を取得ところで

、実際の出力と動作はGHCや他のフラグに依存します。

関連する問題