`

haskell - Input and Output - exceptions

阅读更多

you might be wondering why the exceptions are in the input and outptu chapter, is there  a fit, actually, the resaons why the haskell appears in the input and output chapter has its own reason. 

 

Haskell has different philosohpies from other programming languages where it does not necessary use the exception to represent some error conditions. Language like C uses return value to signal success or failure, while haskell has type such as Maybe, where it can return a meaning value if the everything is all right, and it can return a nohting to signify a failure condition. 

 

Despite having expressive types that support failed computations, Haskell still has support for exceptions, because they make more sense in I/O contexts. A lot of things can go wrong when dealing with the outside world because it is so unreliable. For instance, when opening a file, a bunch of things can go wrong. The file might be locked, it might not be there at all or the hard disk drive or something might not be there at all. So it's good to be able to jump to some error handling part of our code when such an error occurs.

 

so besides I/O, the impure code can throw exceptions, for pure code such as divide by zero is a very common error condition. getting the head of a empty list will throw an exception. 

ghci> 4 `div` 0  
*** Exception: divide by zero  
ghci> head []  
*** Exception: Prelude.head: empty list  

 

so to refine our statement, 

 

ure code can throw exceptions, but it they can only be caught in the I/O part of our code (when we're inside a do block that goes into main). That's because you don't know when (or if) anything will be evaluated in pure code, because it is lazy and doesn't have a well-defined order of execution, whereas I/O code does.

 

  • we will start the discussion beginning with the an doesFileExists exception and
  • an example with doesFileExists exception 
  • try-catch handler in haskell
  • some guideline on whether to choose exception over Maybe values 
-- file 
--   exception101.hs
-- descrpition: 
--  I/O code (i.e. impure code) can throw exception
--   

-- 4 `div` 0

-- head []

-- Pure code can throw exceptions, but it they can only be caught in the I/O part of our code (when we're inside a do block that goes into main).


-- QUOTE:
--  Pure code can throw exceptions, but it they can only be caught in the I/O part of our code (when we're inside a do block that goes into main). That's because you don't know when (or if) anything will be evaluated in pure code, because it is lazy and doesn't have a well-defined order of execution, whereas I/O code does.



-- QUOTE:
--  The logic of our program should reside mostly within our pure functions, because their results are dependant only on the parameters that the functions are called with. When dealing with pure functions, you only have to think about what a function returns, because it can't do anything else. 



-- QUOTE:
-- Pure functions are lazy by default, which means that we don't know when they will be evaluated and that it really shouldn't matter.
-- However, once pure functions start throwing exceptions, it matters when they are evaluated. That's why we can only catch exceptions thrown from pure functions in the I/O part of our code. And that's bad, because we want to keep the I/O part as small as possible. However, if we don't catch them in the I/O part of our code, our program crashes. The solution? Don't mix exceptions and pure code. 

import System.Environment 
import System.IO



-- main = do (fileName : _) <- getArgs 
--    contents <- readFile fileName
--    putStrLn $ "The file has " ++ show (length (lines contents)) ++ "lines!"

-- throws exception when file with the filename provided does not exists



-- solution I : check before act
-- doesFileExist
-- import System.Environment  
-- import System.IO  
-- import System.Directory  
--   
-- main = do (fileName:_) <- getArgs  
--           fileExists <- doesFileExist fileName  
--           if fileExists  
--               then do contents <- readFile fileName  
--                       putStrLn $ "The file has " ++ show (length (lines contents)) ++ " lines!"  
--               else do putStrLn "The file doesn't exist!"  

 

and the try-catch handler in examples.

-- file 
--   ioeGetFileName.hs
-- descrpition: 
--   ioe functions families to let you ask the exceptions some attributes
--   

import System.Environment     
import System.IO     
import System.IO.Error     
    
main = toTry `catch` handler     
                 
toTry :: IO ()     
toTry = do (fileName:_) <- getArgs     
           contents <- readFile fileName     
           putStrLn $ "The file has " ++ show (length (lines contents)) ++ " lines!"     
    
handler :: IOError -> IO ()     
handler e     
    | isDoesNotExistError e =   
        case ioeGetFileName e of Just path -> putStrLn $ "Whoops! File does not exist at: " ++ path  
                                 Nothing -> putStrLn "Whoops! File does not exist at unknown location!"  
    | otherwise = ioError e     


-- or you can add hook more than one handler to the functions
-- main = do toTry `catch` handler1  
--           thenTryThis `catch` handler2  
--           launchRockets 
-- 


-- This is kind of similar to try-catch blocks of other languages
--  I prefer to have their type be something like IO (Either a b), meaning that they're normal I/O actions but the result that they yield when performed is of type Either a b, meaning it's either Left a or Right b.

 

last, let's see the exception hierarchy that you might be able to use i haskel. 

-- file 
--  exception_trycatch.hs
-- descrpition: 
--  cehck to see if the exception is the one that we are expecting 
--   
import System.Environment  
import System.IO  
import System.IO.Error  
  
main = toTry `catch` handler  
              
toTry :: IO ()  
toTry = do (fileName:_) <- getArgs  
           contents <- readFile fileName  
           putStrLn $ "The file has " ++ show (length (lines contents)) ++ " lines!"  
  
handler :: IOError -> IO ()  
handler e  
    | isDoesNotExistError e = putStrLn "The file doesn't exist!"  
    | otherwise = ioError e  





-- the following types of exception that exists below 

-- isAlreadyExistsError
-- isDoesNotExistError
-- isAlreadyInUseError
-- isFullError
-- isEOFError
-- isIllegalOperation
-- isPermissionError
-- isUserError


-- so that you could have a handlers that looks like something like this:

-- handler :: IOError -> IO ()  
-- handler e  
--     | isDoesNotExistError e = putStrLn "The file doesn't exist!"  
--     | isFullError e = freeSomeSpace  
--     | isIllegalOperation e = notifyCops  
--     | otherwise = ioError e  


-- there a series of exception that exists which start with ioe and you can see a full list of them 
--  such as ioeGetFileName
-- 

-- ioeGetFileName :: IOError -> Maybe FilePath

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics