Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

Can we try without catch block in java? if yes , then how it works?

user-image
Question ajoutée par Mohd shahnawaz khan , Associate Project , Cognizant Technology Solution
Date de publication: 2014/01/13
Masud Sarkar
par Masud Sarkar , Software Developer , Amdocs

Yes 1) We can have try without catch but Finally is mandatory2) We cannot have catch without try

but it is not recomended to have a try block without a catch because the finally block is always executed, whether an exception is thrown or not.

Utilisateur supprimé
par Utilisateur supprimé

try {

}

finally

{}

but the problem is finally is the final exicutable statement but we cannot rise any exceptions/user defined exceptions.....

Sandhya Binoy
par Sandhya Binoy , Software Professional , Karrox technologies

The finally block in java code is executed even if the try or catch block contains control transfer statements like return, break or continue.

class JavaFinally{

public static void main(String args[]){

System.out.println(JavaFinally.myMethod());

}

public static int myMethod(){

try

{return112;}

finally {

System.out.println("This is Finally block");

System.out.println("Finally block ran even after return statement");

}}}

You can handle exceptions still without having catch blocks also, only thing you need to do is declare the throws clause in your method signature, so that the calling function would handle the exception. Before throwing exception, it executes the finally block.

 

IRFAN AHMED
par IRFAN AHMED , Programmer Analyst , EWS

YES

try{

}finally{

}

Md Khalid Ahmad
par Md Khalid Ahmad , Senior Java Developer , BRITSH TELECOM

Yes we can have.

Cedric Nabaa
par Cedric Nabaa , Java developer , Capital Banking Solutions

yes.

try{

system.out.println("test")

}finally{

}

 

technically you can, but you should not. finally will always be called but u cant know in which state your object will be in. why dont you want to use a catch?

More Questions Like This

Avez-vous besoin d'aide pour créer un CV ayant les mots-clés recherchés par les employeurs?