Exceptions in Java

Exceptions are a topic in Java that caused me some confusion in the beginning. All I understood for a while was it was a thing you’d have to deal with by adding a try/catch block or by adding a ‘throws’ keyword to a certain kind of exception. I wouldn’t be able to really explain in human language what it is though and that’s not a good position to be in if you had to do a coding-style interview, knew you had to catch an exception and not be able to explain why or what exactly you’re doing to the interviewer. Writing this blog(reading it for your side!) should certainly avoid that problem!

An exception seems similar to an error on the surface: it’s an unwanted event that disrupts the expected flow of a program at run time. In this definition, you can think of an error as being an unofficial type of exception as an error will certainly disrupt the normal flow of a program when you try to execute it!

There are two accepted types of exceptions: checked and unchecked exceptions.

Checked Exceptions are…

  • checked at compile time
  • handled by using try-catch blocks or by declaring the throw keywords

Unchecked Exceptions are…

  • not checked at compile time
  • direct sub-classes of the RuntimeException class
  • occur most often as a result of bad data given by the user in an interaction
  • up to the programmer to decide how to react to these possible bad conditions in advance so the program can handle them correctly

This has been a really quick and dirty look at exceptions. Exceptions are an unwanted event that can throw your program off it’s intended flow. There are two kinds of exceptions, one that is checked at compile time and one that isn’t.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.