我正在尝试捕获当用户写出与预期不同的内容时发生的异常。
我这样做:
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)
答案 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