added exception handling for reading matrices