Java Error and Exception
Understanding Java errors and
exceptions for better coding practices.
By Sahil Chaudhary
01
Errors
Definition
Errors in Java represent serious issues that cannot
typically be handled by the program. They are
subclasses of the 'Error' class and can be
categorized into two main types: 'System Errors',
which arise from environmental factors, and
'Compile-time Errors', which are detected when
Java code is compiled.
Common examples
• OutOfMemoryError — when the computer cannot give your program any more memory.
• StackOverflowError — too much recursion (your program is calling methods forever).
• VirtualMachineError — serious problems inside the JVM.
🧠 Key Points about Errors:
✅ You can’t recover easily from Errors.
✅ They often mean something is very wrong with the environment (JVM or hardware).
✅ Usually, you do not catch Errors — your program will stop.
Error handling techniques
Technique Description Example
Wrap risky code and try { ... }
🔵 try-catch catch exceptions to catch(Exception e)
prevent crashes { ... }
Runs whether an
🧹 finally exception occurs or not finally { closeFile(); }
(e.g. cleanup)
Manually throw an throw new
🚀 throw exception if a condition IllegalArgumentExcepti
is wrong on(...);
Declare a method might public void readFile()
📢 throws
throw an exception throws IOException { }
Auto-close try(FileReader fr = new
📂 try-with-resources
files/resources after use FileReader(...)) { }
02
Exceptions
Definition
Exceptions in Java are events that disrupt the normal
flow of a program's execution. They are objects that
represent an error condition and can be categorized
into two main types: 'Checked Exceptions', which must
be either caught or declared in the method signature,
and 'Unchecked Exceptions', which are not required to
be handled explicitly.
Common examples
• 🔢 ArithmeticException — dividing by zero
• 📄 FileNotFoundException — file you want to read
isn’t there
• 🧠 NullPointerException — using an object that’s null
Exception handling strategies
Strategy What? Example
Catch & handle errors so the try { ... } catch (Exception e)
🔵 try-catch
program doesn’t crash { ... }
Runs always (e.g. clean up
🧹 finally finally { closeFile(); }
files/resources)
throw new
🚀 throw Manually signal an error
IllegalArgumentException(...);
Declare a method might throw void readFile() throws
📢 throws
an exception IOException { }
Auto-close files/resources after try(FileReader fr = new
📂 try-with-resources
use FileReader(...)) {}
Conclusions
In summary, understanding errors and exceptions in Java
is crucial for developing robust applications. Effective
handling techniques not only enhance application
reliability but also improve overall user experience. By
employing proper strategies, developers can ensure that
their applications are resilient to unexpected conditions.
Thank you!
• Do you have any questions?
CREDITS: This presentation template was created by Slidesgo
, and includes icons, infographics & images by Freepik
+00 000 000 000