Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1033 :

Posted: Thu Jan 01, 2015 1:20 pm
by hlosukwakha
I don't understand the explanation given for question ID
enthuware.ocajp.i.v7.2.1033
:

A try statement must always have a ............. associated with it.
:?: :?:

Re: About Question enthuware.ocajp.i.v7.2.1033 :

Posted: Thu Jan 01, 2015 9:28 pm
by admin
You cannot just have a try block. i.e.
try{

some code

}

You also need to have either a catch or a finally or both i.e.

try{
}catch(Exception e){ }

or try{
}catch(Exception e){ }
finally{ }

or
try{
}finally{ }

Re: About Question enthuware.ocajp.i.v7.2.1033 :

Posted: Wed Sep 23, 2015 6:40 am
by sosenz
Its a bit confusing, should be catch or final or both imo

Re: About Question enthuware.ocajp.i.v7.2.1033 :

Posted: Wed Oct 04, 2017 5:09 am
by surabhigupta94
A catch can catch multiple exceptions: try{ } catch(Exception1|Exception2|Exception3 e){  }

shouldn't the catch block be written as:
catch (Exception1 || Exception2 || Exception3 e) { }

Re: About Question enthuware.ocajp.i.v7.2.1033 :

Posted: Wed Oct 04, 2017 6:59 am
by admin
By that logic, why only 3, the catch clause can have any number of Exceptions. Also, it should be | and not ||.
But that is not the point of the question. It is getting at the requirement of having at least a catch block or a finally block.

Re: About Question enthuware.ocajp.i.v7.2.1033 :

Posted: Wed Dec 04, 2019 10:32 am
by pavel.gurinovich
This question is completely correct since it is possible to use try with resources.
Here is example:

Code: Select all

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        try(Scanner s = new Scanner("")){
            System.out.println("Valid try declaration");
        }
    }
}

Re: About Question enthuware.ocajp.i.v7.2.1033 :

Posted: Wed Dec 04, 2019 9:21 pm
by admin
A try statement is not same as try-with-resources statement. Both are different. The question is asking about try statement (not try with resources statement).
Problem statement has now been updated to make it clear.

thank you for your feedback!