Why am I getting errors with my Java try…catch?

后端 未结 6 1324
粉色の甜心
粉色の甜心 2021-01-24 05:09

I\'m starting to teach myself more about Java error handling, and this is my first program where I\'m trying to see specific errors instead of using catch (Exception e)

6条回答
  •  伪装坚强ぢ
    2021-01-24 06:04

    Because the type of error thrown doesn't match the one you're catching. Try this...

    catch(Exception e) {
      System.out.println("Exception: "+ e.getClass());
    }
    

    That will show you the type of error you should be catching. Obviously this isn't good practice but it's a good exercise for seeing what's happening. Other answers on this page concerning checked and unchecked exceptions are pretty concise.

提交回复
热议问题