Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

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

user-image
Question added by Mohd shahnawaz khan , Associate Project , Cognizant Technology Solution
Date Posted: 2014/01/13
Masud Sarkar
by 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.

Deleted user
by Deleted user

try {

}

finally

{}

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

Sandhya Binoy
by 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
by IRFAN AHMED , Programmer Analyst , EWS

YES

try{

}finally{

}

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

Yes we can have.

Cedric Nabaa
by 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