Update index.js

add exception handling