Monday 6 February 2023

Five Java Exceptions Personified

Exceptions in programming; we encounter them a lot of the time. How many of us, when confronted with a thrown exception, immediately think of a person? I know I do. And in today's blogpost, I will be listing five examples.

The exceptions listed below mostly occur in Java, but some are pretty general and should be encountered in basically any programming language.

1. ArrayIndexOutOfBoundsException

This occurs when the programmer attempts to grab an element from an array using an index which does not exist. For example, if the array has only 6 elements and you request for element number 10, this exception would occur.

Out of bounds!

When it comes to "out of bounds", the first thing this brings to mind is a soccer linesman signalling that the ball has gone over the line. Seems pretty apt.

2. FileNotFoundException

Pretty straightforward here; this exception is thrown when the code references a file that does not exist. It could be a wrong filename or directory, either results in the same thing.

No such publication.

What else could I possibly think of at this point, but a stern librarian informing me that a certain book is not available?

3. ArithmeticException

An exception of this sort typically occurs on a bad arithmetic operation, such as a division by zero. There are other instances where this would be thrown, but a division by zero is probably the most common.

Nope, math does not
work like that.

The most obvious image that comes immediately is that of a math teacher telling you your formula is wrong!

4. IllegalArgumentException

This one gets thrown when a method receives an argument that does not fit the parameters allowed. For instance, if a method allowed for two parameters, a string and an integer, the exception would be thrown if we attempted to call that method using two strings.

Court, to order!

This exception is probably best personified by a judge in a court session, bringing down that gavel. "Illegal argument" is kind of like saying "Objection sustained. Prosecution will watch their line of questioning", or something to that effect.

5. ClassNotFoundException

When a programmer tries to invoke a class that doesn't exist, this is what gets thrown. It could be a misspelled class name, or a class that does not belong in the current hierarchy.

You must be
new here?

Can't find your class? This is where the hall monitor or prefect comes in! Sorry, bad pun.

That's all!

This was meant to be a silly, frivolous listicle with a tech twist. There's not supposed to be any deep meaning to it! I hope you had fun.

Stay exceptional!
T___T

No comments:

Post a Comment