Some Quick and Easy Exception Handling (Using Try-Catch Statements)

This is just a quick article about a bit of exception handling we can do when programming 😉 Admittedly, there’s a time and a place for just doing some null checks but sometimes you’ll want the program to intervene a little more when something’s not quite right. A try-catch statement containing a method’s code can offer just this:

This sort of usage will let us know where an error occurred (or, at least, the method it occurred in), and any information contained within the exception we catch.

If your method is on the large side, you may well want to isolate this further, and you’re welcome to do some exception-handling nesting such as this:

You don’t need some of this “Exception e” stuff if you’re not interested in printing out the error information; you can just use these nested try-catch statements to catch exceptions without the entire method’s try-catch statement from triggering. Some standard null-checking can also do the trick with this:

Lastly, something this got me thinking about is considering what’s most appropriate for the situation. I’m sometimes wondering whether it’s more important for the program to do its best to continue in spite of an exception or whether it’s better for it to essentially stop the program before it can do any damage. I appreciate the latter might be the more correct answer, but realistically I do feel there are going to be plenty of occasions where you simply need things to hold together for the user experience (unglamorous as that sounds). Of course in an ideal world nothing will go wrong with your program and everything will work just fine, but since this isn’t that world it feels important to prepare for the worst.

Some of this comes to mind as a result of some programming I’m doing and working with at the moment, which ends up going onto essentially a number of microcontrollers. These really can’t handle exceptions themselves, and they may well need replacing or repairs if your code is sufficiently faulty. In these cases, I feel I can more safely say that I want the program to stop what it’s doing in the event of an exception rather than continue 😅 But anyway, I hope that’s some food-for-thought.

--

--

--

Game Developer | Game Design and Literature Graduate

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Where to get good developers? Ukraine!

Flutter: Cubit For State Management

Google Kubernetes Networking options explained & demonstrated

Single Linked List implementation in Scala

Understanding the SQL as a Newbie

Mock verification in Kotlin using MockK and Atrium

Exatlon — Hack the Box walkthrough

OPPs in JAVA

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Marcus Ansley

Marcus Ansley

Game Developer | Game Design and Literature Graduate

More from Medium

Image classification in a DotVVM web application with ML.NET

How to use Post Processing in Unity

Load data into Amazon Cloud

How I used scriptable objects to build a powerup system