Uploaded by mccarty.steve94

Exceptions

advertisement
4/26/2021
Exceptions
In this paper I am going to describe what an exception is in relation to Java,
describe the differences between checked and unchecked exceptions, explain the
importance of handling checked exceptions, and explain how to handle exceptions in
Java programs.
An exception in Java is something triggered by your application which interferes
with the normal operation of the program. If I had to compare it to something personally
relatable it would be a weapon malfunction, of which like exceptions there are many
potential causes, for example failure to eject or double feeds. Some examples of things
that cause exceptions in Java could be invalid data entered by users, corrupt or missing
files, loss of network connection.
To differentiate between checked and unchecked exceptions let’s start with a
summarization of what they are. Checked exceptions are something that will prevent the
program from starting in the first place. The compiler will notice this exception on
runtime and unless handled will cause an error. An example of checked exceptions that
could be encountered would be from corrupted/missing files. An unchecked exception is
an exception that occurs while the program is in operation, and thus is a result of an
error that is not “checked” by the compiler prior to the program launching. An example
of an unchecked exception would be that of invalid data entered by the user, loss of
internet connection, or failed attempt to access a damaged or missing file.
It is important that checked exceptions are handled so that the program will
function. If they are not the program will fail to compile completely. This prevents further
debugging of the program as well as completely prevents users from accessing the
program.
My plans for handling exceptions in my own java programs focus primarily on
validation on input as well as try catch statements when it comes to accessing files or
different classes/ parts of the program. I will try to anticipate potential exceptions using
the throw keyword to address them ahead of time for checked exceptions.
Sources:
Oracle:
https://docs.oracle.com/javase/tutorial/essential/exceptions/definition.html#:~:text=Definit
ion%3A%20An%20exception%20is%20an,off%20to%20the%20runtime%20system.&text=This
%20block%20of%20code%20is%20called%20an%20exception%20handler.
GeeksforGeeks:
https://www.geeksforgeeks.org/checked-vs-unchecked-exceptions-in-java/
https://www.geeksforgeeks.org/throw-throws-java/
BeginnersBook:
https://beginnersbook.com/2013/04/java-checked-unchecked-exceptions-with-examples/
Download