Ganesh NB
Ganesh NB

Reputation: 81

Priority for Try-catch with Throws in Java

I heard that try+catch or throws can be used to throw an exception. But if they both are used in a context which one will be executed.

public static void main(String args[]) throws IOException
{
    try
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String s=br.readLine();
    }
    catch(IOException e)
    {
        System.err.println(e);
    }
}

Upvotes: 1

Views: 4091

Answers (5)

Hoopje
Hoopje

Reputation: 12932

An exception can be thrown and handled (caught). The first is done either explicitly by the throw keyword or implicitly (for example, if you divide an integer by 0). The second is done by a try...catch block. The throws clause mentioned by you neither throws nor handles an exception (see below).

If an exception is thrown, normal execution stops immediately and the appropriate catch block is executed. After that, execution proceeds after the try...catch block.

So what does the throws clause do? In some cases, you don't want to handle an exception yourself. You want the exeception to propagate to the code that calls your method so that it can handle the exception and take appropriate action. The throws clause says nothing more than: "Beware! This method may (not must) throw an exception of the given type, so you'd better add error handling for it!". (In some cases, in particular if your code or code called by your code throws a so-called checked exception but does catch it, it is required to add a throws clause to the method declaration; in other cases it is optional.)

In your case, the code

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s=br.readLine();

can throw an IOException. Since the code occurs within in try block which contains a catch block IOExceptions, it is not necessary to add the throws clause. It is allowed, though, although in your case it is spurious because your code cannot throw an unhandled IOException.

Upvotes: 0

Gaur93
Gaur93

Reputation: 695

Athrowsclause is used to declare any checked exception classes (§11.1.1) that the statements in a method or constructor body can throw (§11.2.2). Whereas catch block is used for catching all types of Exceptions. An exception thrown from a try block will be caught in catch block.
So here catch block will be executed

Upvotes: 0

Jude Niroshan
Jude Niroshan

Reputation: 4460

If your program en counted a exception inside a try{} it will jump to catch(Exception e){} block.

If you occurred an exception inside your method but outside try{} and catch(Exception e){} it will throws it out of the method.

Whether the method will goes to catch(Exception e){} block or throws ; that will depend on the place where the exception is occurring.

Upvotes: 2

Eran
Eran

Reputation: 393846

Catching an exception and declaring it in a throws clause are not "used to throw an exception". They are the two ways you can handle a checked exception that might be thrown inside your method (or by a method called by your method). You can either handle it inside the method by catching it, or declare it in a throws clause, to let the users of your method know they should handle it.

If you are catching all the exceptions in your method (as catch(Exception e) does), there's no meaning to having a throws clause, since your method will never throw an IOException.

Upvotes: 2

Jens
Jens

Reputation: 69450

In your case the catch block will be executed.

Upvotes: 0

Related Questions