나는 readFile
함수에 간단한 처리기를 추가하려고 해요 : 따라서 readHandler :: IOError -> IO()
를예외 'ReadFile을'
Couldn't match type `()' with `[Char]'
Expected type: IOError -> IO String
Actual type: IOError -> IO()
In the second argument of `catch', namely `readHandler'
In a stmt of a 'do' block:
contents <- (readFile "path") `catch` readHandler
In the expression:
do { contents <- (readFile "path") `catch` readHandler;
putStrLn contents }
:이 컴파일 할 때
readHandler :: IOError -> IO()
readHandler e
| isDoesNotExistError e = putStrLn "The file does not exist"
| otherwise = putStrLn "Something went wrong"
main = do
// stop executing if there is no file
contents <- (readFile "path.xml") `catch` readHandler
// generates [(x,y),(x,y)]
coordinates = parseXML contents
// I want to do something with the coordinates
nextFunction coordinates
내가 오류 readHandler :: IOError -> IO String
이어야합니다.
하지만이 방법으로 오류 메시지를 인쇄 할 수 없습니다.
는이 문제를 어떻게 해결해야합니까?
내가 더 명확하게 코드를 편집했습니다. 내 처리기로 두 번째 솔루션을 사용하고 파일이 발견되면 실행 체인을 계속 수행 할 수 있습니까? –
@ Gert-Jan 물론, 문자열을 출력하는 대신에, 그 경우 분기를위한 문자열을 사용하고자하는 것을 넣으십시오. 이것은 물론 추한 것입니다. 불합리한 접근법은 '모세'모나드입니다. – jozefg
이제 문제는 'strOrExc'이 'String'대신 'aither a0 String'유형입니다.나는 하스켈에 대한 나의 지식이 아직이 문제를 해결하기에 충분하지 않다고 생각한다. 힘든 도움에 감사드립니다! :) –