🌐
GeeksforGeeks
geeksforgeeks.org › java › exceptions-in-java
Java Exception Handling - GeeksforGeeks
Exception Handling in Java is a mechanism used to handle both compile-time (checked) and runtime (unchecked) exceptions, allowing a program to continue execution smoothly even in the presence of errors.
Published   April 1, 2026
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › exceptions › index.html
Lesson: Exceptions (The Java™ Tutorials > Essential Java Classes)
See Java Language Changes for a ... and removed or deprecated options for all JDK releases. The Java programming language uses exceptions to handle errors and other exceptional events....
Discussions

Java exception handling - Stack Overflow
Basically even if no exception is raised the finally code is still run. You may wonder the point in this, but its often used with streams/file handling etc to close the stream. Read more on java exceptions here in tutorials written by Sun (now Oracle)- http://download.oracle.com/javase/tut... More on stackoverflow.com
🌐 stackoverflow.com
java - The best way to handle exceptions? - Software Engineering Stack Exchange
If your program doesn't catch the exception and handle it then the Java runtime needs to. And if the runtime cannot, then the host OS needs to, which should terminate the application. And if the host OS doesn't, then... I dunno. Reboot. I'm just guessing, but since this is an example project for learning purposes, I imagine you don't have a full framework, which means you need to write the global error handling logic. When you don't see a better place, put it in ... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
November 13, 2024
Best practice for Java exception handling - Stack Overflow
The only exceptions you should ... to handle. Out of your list of exceptions, the only one you should bother with is IOException. The rest are the result of not enough tests, or sloppy coding; those shouldn't occur in the normal run time of your application. Third, in Java 7, you have ... More on stackoverflow.com
🌐 stackoverflow.com
Exception Handling Guide in Java
Devs are just too lazy to think about proper error handling. They just don't like the look of "verbose" throws and try-catches. There I called it. Checked exceptions are a nice instrument for situations where you have a recoverable business error and no result object to integrate the error into. Anyway, be it runtime exception or checked exception or result object or Either or Try, a business error must be part of the API. They are not an abstraction leak. Runtime exception is the only one of the mentioned above that can be - and often is - made invisible to the caller. More on reddit.com
🌐 r/java
45
30
January 24, 2021
🌐
Medium
medium.com › @ShantKhayalian › mastering-java-exception-handling-a-comprehensive-guide-for-developers-c31cef921d75
Mastering Java Exception Handling: A Comprehensive Guide for Developers | by Shant Khayalian | Medium
June 17, 2024 - Java Exception Handling is a mechanism to handle runtime errors, allowing a program to continue operating or terminate gracefully. It separates error-handling code from regular code, enhancing readability and maintainability.
🌐
W3Schools
w3schools.com › java › java_try_catch.asp
Java Exceptions (Try...Catch)
When an error occurs, Java will normally stop and generate an error message. The technical term for this is: Java will throw an exception (throw an error). Exception handling lets you catch and handle errors during runtime - so your program ...
🌐
Baeldung
baeldung.com › home › java › core java › exception handling in java
Exception Handling in Java | Baeldung
December 17, 2025 - More on both of these in a moment. Oracle’s documentation tells us to use checked exceptions when we can reasonably expect the caller of our method to be able to recover. A couple of examples of checked exceptions are IOException and ServletException. Unchecked exceptions are exceptions that the Java compiler does not require us to handle.
🌐
CodingNomads
codingnomads.com › blog › java-exception-handling-in-java
An Introductory Guide to Exception Handling in Java
Exception handling in Java programmatically handles runtime errors in order to avoid disrupting the program’s normal flow. By using exception handling, we can build our programs to take appropriate actions and keep our applications from crashing when an error or exception occurs.
🌐
JetBrains
blog.jetbrains.com › home › intellij idea › easy hacks: how to handle exceptions in java
Easy Hacks: How to Handle Exceptions in Java - The JetBrains Blog
April 17, 2024 - The try...catch block is the primary mechanism for handling Java exceptions. Code that might throw an exception is placed within the try block, and the catch block handles the exception or its inheritors as appropriate.
Find elsewhere
🌐
j-labs
j-labs.pl › home › tech blog › java exception handling: strategies and best practices
Java Exception Handling in Java Best Practices | j‑labs
December 9, 2025 - Java’s exception handling mechanism involves try-catch blocks, where the code that might throw an exception is enclosed in a try block, and a possible exception handling code is placed in corresponding catch blocks.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Exception.html
Exception (Java Platform SE 8 )
April 21, 2026 - Constructs a new exception with the specified detail message, cause, suppression enabled or disabled, and writable stack trace enabled or disabled. ... cause - the cause. (A null value is permitted, and indicates that the cause is nonexistent or unknown.) enableSuppression - whether or not suppression is enabled or disabled · writableStackTrace - whether or not the stack trace should be writable ... Java™ Platform Standard Ed.
🌐
Edureka
edureka.co › blog › java-exception-handling
Exception Handling in Java | A Beginners Guide to Java Exceptions
July 26, 2023 - Java exception handling will give you a insight on various types of exceptions and the methods to handle them using try, catch, throw, throws and finally.
🌐
Vlabs
java-iitd.vlabs.ac.in › exp › exceptions › theory.html
Exception Handling in Java - Virtual Labs
In Java, an exception is an event that disrupts the normal flow of the program. It is an object which is thrown at runtime. Exception Handling is a mechanism to handle runtime errors such a ClassNotFoundException, IOException, SQLException, RemoteException, etc.
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Java-Exception-handling-best-practices
Java Exception handling best practices
Throw early and handle exceptions late. Don’t log and rethrow Java exceptions. Check for suppressed exceptions. Explicitly define exception in the throws clause.
Top answer
1 of 5
12

For the purposes of this method that returns a list, just don't catch exceptions. Let it throw.

Unhandled exceptions in your application still need to be handled, but the error handling occurs much deeper in the call stack. Something closer to main() will essentially wrap the entire application in a try/catch block where it simply prints the message and stacktrace to the error output.

static int main(String[] args) {
    try {
        // CSV logic/application logic goes here

        return 0;
    }
    catch (Exception ex) {
        System.err.println(ex.toString());

        return 1;
    }
}

This is a fairly consistent pattern across languages and tech stacks. Something closer to the start of the program catches everything and either logs the problem or prints it to the error output of the host OS. This detail is often hidden from us programmers by application frameworks, but it still exists. If your program doesn't catch the exception and handle it then the Java runtime needs to. And if the runtime cannot, then the host OS needs to, which should terminate the application. And if the host OS doesn't, then... I dunno. Reboot.

I'm just guessing, but since this is an example project for learning purposes, I imagine you don't have a full framework, which means you need to write the global error handling logic. When you don't see a better place, put it in main() or whatever function represents the start of a new thread, but keep the error handling logic simple. At that point you just need to report the problem for debugging purposes, especially for toy projects. If there is no way to recover and continue a use case, terminate the program.

In cases where retrying a use case is desirable, then you need to catch exceptions closer to the logic for that use case. For example, in the controller of an MVC application. This allows you to redraw the UI with an error message affording the user an opportunity to correct their input and try again.

2 of 5
12

Sometimes the best way to handle an exception is not at all.

Since I only want the error and stack trace to be printed in the console and the application to be terminated, what is the best approach to deal with this?

This looks like a job for a throws declaration. In Java, if you're throwing an (ick) checked exception out of the method then you need to say so in the method signature. FileReader throws a FileNotFoundException which is a checked exception.

public static List<Usuario> ler() throws FileNotFoundException {
    try (FileReader reader = new FileReader(UsuarioArquivo.getCaminho().toString())) {
        return new CsvToBeanBuilder<Usuario>(reader)
            .withType(Usuario.class)
            .build()
            .parse()
        ;
    }
}

Throwing from within a try-with-resources can get interesting but I believe this is all you need here.

This is not for a real application; it's just a simple study project, so if something goes wrong, I just want to know what happened and have the program terminate, nothing more.

Real or not, depending on how you're running the program, just letting the exception go uncaught may already do exactly that. Halting a program that is in an undefined state is a good thing. If you can't recover and put it back into a defined state then it's best to let it halt before it corrupts data, causes a security issue, or sends the president threating emails.

Now maybe you can recover. Either by rejecting the requested task or performing the task in some degraded way. Would your program react well if you simply returned an empty list?

I don't want to handle them in my application, mainly because I don't even know how to handle them.

Well that’s a bad reason to not handle them. Handle them wherever you can see what to do next. Something you were counting on didn’t work out. Can you see what to do next?

Easily 80% of code is just dealing with things going weird. Don’t be surprised if this takes a fair bit of work.

🌐
Pressbooks
pressbooks.pub › javaprogramming › chapter › exception-and-error-handling-java-programming
Exception and Error Handling — Java Programming – Java Programming
Java exception handling is done through: try: Statement that should be mentioned for exceptions should be put in the try block, If the exception occurs in try block, then it is thrown.
🌐
Scaler
scaler.com › home › topics › java › exception handling in java
Exception Handling in Java - Scaler Topics
April 13, 2022 - JVM(Java Virtual Machine) by default handles exceptions, when an exception is raised it will halt the execution of the program and throw the occurred exception. We handle exceptions in java to make sure the program executes properly without ...
🌐
Medium
medium.com › @anuj.gupta618 › mastering-exception-handling-in-java-a-comprehensive-guide-for-beginners-76cbc1e985d9
Mastering Exception Handling in Java: A Comprehensive Guide for Beginners | by Anuj Gupta | Medium
January 26, 2025 - Checked Exceptions: These are exceptions that the compiler forces you to handle (e.g., IOException, SQLException). Unchecked Exceptions: These are runtime exceptions that are not checked at compile time (e.g., NullPointerException, ArrayIndexOutOfBoundsException). Java uses a few keywords to handle exceptions effectively.
🌐
Programiz
programiz.com › java-programming › exception-handling
Java Exception Handling (With Examples)
To handle the exception, we have put the code, 5 / 0 inside the try block. Now when an exception occurs, the rest of the code inside the try block is skipped. The catch block catches the exception and statements inside the catch block is executed. If none of the statements in the try block ...
Top answer
1 of 2
23

First, unless you have very good reason, never catch RuntimeException, Exception or Throwable. These will catch most anything that is thrown, and Throwable will catch everything, even those things you're not meant to catch, like OutOfMemoryError.

Second, avoid catching runtime exceptions unless it directly impedes with the critical operation of your program. (But seriously, if anyone sees you catch a NullPointerException, they are within their full rights to call you out on it.) The only exceptions you should be bothering with are those that you are required to handle. Out of your list of exceptions, the only one you should bother with is IOException. The rest are the result of not enough tests, or sloppy coding; those shouldn't occur in the normal run time of your application.

Third, in Java 7, you have the ability to do a multi-catch statement for your exceptions, if the exceptions are mutually exclusive. The example linked does a good job of explaining it, but if you were to encounter code that threw both an IOException and an SQLException, you could handle it like this:

try {
    // Dodgy database code here
catch (IOException|SQLException ex) {
    logger.log(ex);
    throw ex;
}

This cleans things up a bit, as you don't have unwieldy and huge chains of exceptions to catch.

2 of 2
19

First of all the problem with "best practice" advice is that it tends to over-simplify the question and the answer. Then someone (like yourself) comes along and notices that it is contradictory1.

IMO, best practice is to take "best practice" advice and people who regularly use that phrase with a healthy level of suspicion. Try to understand the real issues yourself, and reach your own conclusions ... rather than just relying someone else to tell you what is "best practice".

1 - Or worse ... they don't notice the contradictions, edge-cases, etc, and blindly follow the so-called "best practice". And occasionally, they find themselves in a dark place, because the "best practice" recommendation was inappropriate.


So what's the problem here? It is this statement:

but I also heard that catching generic exception is not a good coding practice.

In fact, it is not normally good coding practice to catch generic exceptions like Exception. But it is the right thing to do in some circumstances. And your example is one where it is appropriate.

Why?

Well lets look a case where catching Exception is a bad idea:

    public void doSomething(...) {
        try {
            doSomethingElse(...);
        } catch (Exception ex) {
            // log it ... and continue
        }
    }

Why is that a bad idea? Because that catch is going to catch and handle unexpected exceptions; i.e. exceptions that you (the developer) did not think were possible, or that you did not even consider. That's OK ... but then the code logs the exception, and continues running as if nothing happened.

That's the real problem ... attempting to recover from an unexpected exception.

The (so-called) "best practice" advice to "never catch generic exceptions" deals with the issue, but in a crude way that doesn't deal with the edge cases. One of the edge cases is that catching (and logging) a generic exception is OK if you then immediately shut the application down ... like you are doing.

    public void main(...) {
        try {
            // ...
        } catch (Exception ex) {
            // log exception
            System.err.println("Fatal error; see log file");
            System.exit(1);
        }
    }

Now contrast that with the (supposedly) good practice version in your question. What is the difference?

  1. Your version produces more user friendly / less alarming diagnostics ... up to a point.
  2. Your version is significantly more code.
  3. Your version is unhelpful to someone trying to diagnose the problem because the stacktraces are not recorded.

And the counterpoints to 1 and 2 are:

  1. You can spend limitless time honing the "user friendly" diagnostics for an application, and still fail to help the kind of user who can't or won't understand ...
  2. It also depends on who the typical user is.

As you can see, this is far more nuanced than "catching generic exceptions is bad practice".

🌐
Reddit
reddit.com › r/java › exception handling guide in java
r/java on Reddit: Exception Handling Guide in Java
January 24, 2021 - Also described in Effective Java Item 43: Throw exceptions appropriate to the abstraction. The File API and IOException are always the bad examples analysed in those arguments. ... Use values (return Either or similar) for business errors that must be handled by caller (e.g.