kevinyyh
kevinyyh

Reputation: 1

Can we say that the try-catch block is the only way to handle an exception in java?

So basically, if we use the throws keyword in a method signature, we are passing the handling responsibility to the caller method; if we use the throw keyword, we are explicitly throwing an exception. Both throws and throw are not ways to handle an exception. Only the statements in the catch block can be considered the way to handle an exception since our program won't terminate since we have handled the exception. Am I getting it correctly? Since in Java interviews we are always asked how do we handle exceptions in Java.

Upvotes: 0

Views: 1088

Answers (4)

Timur Efimov
Timur Efimov

Reputation: 358

Shortly, answer ordered like that:

  1. try-catch
  2. Thread::setUncaughtExceptionHandler
  3. ThreadGroup::uncaughtException(Thread, Throwable)
  4. [Globally] Thread.setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler)

Please note, that this list is more about JVM mechanisms rather then 'frameworks'.

If you want more I believe you can play with code below and see docs of used methods and classes.

public static void main(String[] args) throws InterruptedException {
    System.out.println("START main");

    Thread.setDefaultUncaughtExceptionHandler((t, e) -> {
        System.err.println("Thread.setDefaultUncaughtExceptionHandler");
    });
    Thread.currentThread().setUncaughtExceptionHandler((t, e) -> {
        System.err.println("thread[main].uncaughtException");
    });

    java.util.concurrent.ThreadFactory threadFactory = r -> {
        ThreadGroup g = new ThreadGroup("eta") {
            @Override
            public void uncaughtException(Thread t, Throwable e) {
                System.err.println("threadGroup.uncaughtException");
            }
        };
        Thread thread = new Thread(g, r);
        thread.setUncaughtExceptionHandler((t, e) -> {
            System.err.println("thread.uncaughtException");
        });
        return thread;
//            return new Thread(r);
    };

    threadFactory.newThread(() -> {
        System.out.println("START newThread logic");
        throw new RuntimeException();
    }).start();

    if (true) throw new RuntimeException();
}

Ouputs:

START main
START newThread logic
thread[main].uncaughtException
thread.uncaughtException

Upvotes: 0

Mureinik
Mureinik

Reputation: 310993

This is correct.

When an exception is thrown, you have only two options - either catching or allowing it to be thrown upwards to the caller. Ultimately, if it's never caught, the thread will terminate.

Upvotes: 1

Roman C
Roman C

Reputation: 1

Exceptions are handled with try-catch block which wraps the code throwing an exception. No matter how it's issued with throw clause or indirectly via calling a method which has throws in the method signature. If the method throws an exception and does not declare it in the method signature then this is kind of unchecked exceptions. These exceptions are handled the same way as checked exceptions that you have to catch or declare to be thrown.

See more about exceptions in Java Tutorial.

Upvotes: 0

Mmm Tasty Mmm
Mmm Tasty Mmm

Reputation: 11

Yeah I would agree mostly agree.

  • You can catch the exception, and then the exception and handle that (possibly with use of a finally block as well so maybe mention that).
  • Throw upwards again.
  • If you're doing threading you can also set the uncaught exception behavior, so you could "handle" the exception that way in a sense, but that is a specific situation and not always applicable.

Upvotes: 1

Related Questions