什么是异常类型“Exception:Prelude.read:no parse”

时间:2017-03-30 16:27:25

标签: haskell exception-handling

我正在尝试捕获当用户写出与预期不同的内容时发生的异常。

我这样做:

  public function destroy($id)
{
    $category = Category::find($id);
    $category->delete();
    Session::flash('success', 'The category was successfully deleted.');
    return redirect()->route('categories.index');
}

我没有SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`fitilicious`.`products`, CONSTRAINT `products_category_id_foreign` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`)) (SQL: delete from `categories` where `id` = 2)

中需要捕获的异常类型

1 个答案:

答案 0 :(得分:0)

执行readLn时获得的异常类型为IOError,它只是IOException.的别名

以下是处理错误的方法,我已将类型添加到readLn以强制将其作为Int读取。

import           Control.Exception

main :: IO ()
main = catch takeNumber handleError

takeNumber :: IO ()
takeNumber = do
  putStrLn "Give me a number:"
  n <- readLn :: IO Int
  putStrLn ("Success, your number is: " ++ show n)

handleError :: IOError -> IO ()
handleError e = do
  putStrLn ("Invalid number! Try again. The error was: " ++ show e)
  main