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

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
hlosukwakha
Posts: 3
Joined: Thu Jan 01, 2015 1:14 pm
Contact:

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

Post 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.
:?: :?:

admin
Site Admin
Posts: 10065
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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{ }
If you like our products and services, please help us by posting your review here.

sosenz
Posts: 1
Joined: Wed Sep 23, 2015 6:38 am
Contact:

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

Post by sosenz »

Its a bit confusing, should be catch or final or both imo

surabhigupta94
Posts: 1
Joined: Wed Oct 04, 2017 5:06 am
Contact:

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

Post 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) { }

admin
Site Admin
Posts: 10065
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

pavel.gurinovich
Posts: 13
Joined: Mon Nov 18, 2019 4:59 am
Contact:

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

Post 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");
        }
    }
}

admin
Site Admin
Posts: 10065
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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!
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 46 guests