Mateusz Kaflowski
Mateusz Kaflowski

Reputation: 2367

When do I have to surround with try/catch?

I have some piece of code:

public static void a() throws NumberFormatException {
        return;
    }

    public static void b() throws InterruptedException{
        return;
    }

    public static void main(String[] args) {

        a();
        b();

    }

And Eclipse says that I have to surround try/catch to function call b(), but I do not have to do that with a() call. Why? How to make function make to surround with try and catch when is called?

Upvotes: 2

Views: 4727

Answers (9)

Apurv
Apurv

Reputation: 3753

Because InterruptedException is a checked exception and NumberFormatException is an unchecked expcetion.

For checked exception, compiler forces you to either surround them with try-catch block or declare them with throws, while for unchecked exceptions try-catch block is not compulsory.

As function a() throws NumberFormatException (unchecked exception) compiler do not force you to surround the function call with try-catch.

As function b() throws InterruptedException (checked exception) compiler forces you to either surround the function call with try-catch or declare them with throws.

Upvotes: 6

kholofelo Maloma
kholofelo Maloma

Reputation: 1003

There are two ways in Java to handle exceptions -

1. surround try/catch
2. propagate exeption

you use try/catch block if you want to handle this exception locally. Basically, within the try block is a code that has a potential of throwing an exception.

but you use throws SomeException in the method signature if you want this possible exception to be handled by the calling method... this calling method can also propagate this exception further up... should all methods never try/catch this exception, it will be thrown by the Java Virtual Machine at execution time

Upvotes: 0

CloudyMarble
CloudyMarble

Reputation: 37566

The Keyword throws declares that a method may throw that exception. While checked exceptions have to be handled with unchecked exceptions this is optional. In your case a throws a NumberFormatException which is derived from RuntimeException which is according to the definition an unchecked exception, see Section 11.2: Compile-Time Checking of Exceptions:

The unchecked exceptions classes are the class RuntimeException and its subclasses, and the class Error and its subclasses. All other exception classes are checked exception classes.

And according to the definition the InterruptedException which class b throws is a checked exceptions (must be caught in a try-catch block).

Upvotes: 0

codeMan
codeMan

Reputation: 5758

NumberFormatException is not a checked Exception(i.e because it has java.lang.RuntimeException as its base class), which means that the developer is not forced to handle(its the developer's choice whether to handle it or not) when the developer chooses not to handle the exception and if the exception occurs, it will be handled by the JVM.

Where as InterruptedException (i.e because it has java.lang.Exception as its base class) is a checked exception which means that the developer has to handle it every time either by using a try-catch block or by declaring the exception in the Throws clause.

Take a look at this example.

Upvotes: 2

Gab
Gab

Reputation: 8323

All Exception that extends RuntimeException do not imply an explicit try-catch block. They are called Unchecked. Those exception were theoretically reserved to unrecoverable runtime errors thrown by VM. However they are now commonly used as lot's of people agree to say the Checked exceptions mechanism is a bad design concept.

You will commonly use Unchecked when your program cannot recover from the error and let them propagate to a transverse layer exception handling that will be in charge to properly log and / or display the error to user.

Checked exception are commonly used in cases where the caller will be expected to handle the error, because it is able to recover from it or will trigger specific behavior.

Upvotes: 0

Puce
Puce

Reputation: 38132

NumberFormatException is a RuntimeException -> unchecked

InterruptedException is a checked Exception

I found the Java Tutorial quite useful to learn about Java and its APIs, and in fact, I still use it from time to time:

http://docs.oracle.com/javase/tutorial/essential/exceptions/index.html

http://docs.oracle.com/javase/tutorial/

Upvotes: 1

user2243194
user2243194

Reputation:

public class InterruptedException extends Exception Thrown when a thread is waiting, sleeping, or otherwise occupied, and the thread is interrupted, either before or during the activity. Occasionally a method may wish to test whether the current thread has been interrupted, and if so, to immediately throw this exception.

but for the first case it will not ask NumberFormatException is an unchecked expcetion.

Upvotes: 0

Miloš Lukačka
Miloš Lukačka

Reputation: 842

because NumberFormatException() is unchecked exception InterruptedException is checked, you have to surroud checked exceptions with try-catch, but you dont have to try-catch checked - every method would have try-catch then.

Upvotes: 1

Andremoniy
Andremoniy

Reputation: 34900

This is because NumberFormatException extends RuntimeException - i.e. it is unchecked exception. While InterruptedException is checked exception - it should be catched in try...catch block, or you should add throws to main() method.

Upvotes: 1

Related Questions