Java Exceptions and Get Methods

Gavin Wiener
1 min readJan 25, 2018

--

Originally posted on my blog of random tid-bits and Android APK breakdowns — Which Android Libraries?

I recently learned Java Exceptions have three different get methods. Throughout my coding practices and learning, I have used e.printStackTrace();.

I also could not find a clear example of the different outputs, this short article will outline those outputs.

File Read Exceptions

This example is a simple file reader in a try-catch block.

printStackTrace

java.io.FileNotFoundException: <file path> (No such file or directory)at java.io.FileInputStream.open0(Native Method)at java.io.FileInputStream.open(FileInputStream.java:195)at java.io.FileInputStream.<init>(FileInputStream.java:138)at java.io.FileInputStream.<init>(FileInputStream.java:93)at java.io.FileReader.<init>(FileReader.java:58)at FileReadException.main(FileReadException.java:11)

toString

java.io.FileNotFoundException: <file path> (No such file or directory)

getMessage

<file path> (No such file or directory)

Logic Exceptions

This example simulates a run-time error, I tried to divide by zero in try-catch block.

printStackTrace

java.lang.ArithmeticException: / by zero at ExceptionTest.main(ExceptionTest.java:5)

toString

java.lang.ArithmeticException: / by zero

getMessage

/ by zero

If you found this tutorial helpful, I use Twitter to post more lessons about automation that will help you save time and grow your business. You can follow me here.

--

--

Gavin Wiener
Gavin Wiener

Written by Gavin Wiener

I'm a coder and freelancer from South Africa with 5+ years experience in automations, integrations and API's using Python and Django.

No responses yet