ss123
ss123

Reputation: 31

Is it necessary to put catch statements after a try-block?

I just want to know is it necessary to put catch after try block, or can we use try blocks without a catch block?

Upvotes: 2

Views: 10241

Answers (8)

Logos
Logos

Reputation: 382

First thing to remember is that you have to know what the purpose of the try-catch-finally block is.

  1. The try block is used to test the code written inside it. If the code causes an exception, it throws the exception to the catch block.
  2. The catch block is used to handle the thrown exception like, assume that you wrote a code that prompt the user to insert numbers only. But the user inputted a letter, thus the code throw an exception. The exception then would be caught by the catch block. Then the catch block prompt the user to re-input the data. This is what you call exception handling. But if you want to just leave the catch block empty is fine.
  3. You may write try without the catch keyword following it but, you have to write the finally after the try block.
  4. The code in the finally block will always be executed no matter what. You usually write codes in the finally block to close resources opened in the try block like files or database connection.
  5. You can use the try-with-resources in place of the finally block (available in java 8).

So, you can write try followed by catch then followed by finally like the following example :

try{
    //code
}
catch(Exception ex){
    //code to handle the problem.
}
finally{
    //Closing resources etc.
}

Or You can write this :

try{
    //code
}
catch(Exception ex){
    //code to handle the problem.
}

Or this :

try{
    //code
}
finally{
    //Closing resources etc.
}

But, you usually would want to handle the problem with the catch block.

Upvotes: 0

Balban
Balban

Reputation: 736

Yes you can... but you must put a finally block after try. So you can do it like this:

try
{
}
finally
{
}

or

try
{
}
catch(Exception e)
{
}

Upvotes: 1

nightmare17
nightmare17

Reputation: 21

Yes you can use finally instead but to be more practical I use "throws Exception" function if I can because using try and catch blocks makes code harder to read.

Upvotes: 0

irreputable
irreputable

Reputation: 45433

In Java 7 the try-with-resource statement doesn't need catch or finally clause

try(InputStream is = new FileInputStream(..))
{
    is.read();
}

Upvotes: 0

Saurabh Gokhale
Saurabh Gokhale

Reputation: 46395

is it necessary to put catch after try block ?

Nope, not at all. Its not mandatory to put catch after try block, unless and until the try block is followed by a finally block. Just remember one thing, after try, a catch or a finally or both can work.

we can use try without catch block?

Yes, you can. But that will be a bad practise. Since, you are writing a try block, you should be writing catch block ( for catching the exception) and a good practise to follow it by a finally block.

Upvotes: 1

Jim Deville
Jim Deville

Reputation: 10662

try without a catch block is a syntax error because it makes no sense (unless you also want to use a finally block). The only reason to use try is in order to catch the exception (or do a finally) from within that block

Upvotes: 0

Petar Ivanov
Petar Ivanov

Reputation: 93010

You need to put either catch or finally block after try.

try {

}
finally {

}

or

try {

}
catch (Exception e) {

}

Upvotes: 5

Harry Joy
Harry Joy

Reputation: 59650

Yes you can write try without catch. In that case you require finally block. Try requires either catch or finally or both that is at least one catch or finally is compulsory.

try{
  // throw exception
} finally{
  // do something.
}

But you should avoid this case cause in this case you will loose exception details. So if you don't want to handle it in here then simply throw that exception.

Upvotes: 0

Related Questions