ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

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

user-image
تم إضافة السؤال من قبل Mohd shahnawaz khan , Associate Project , Cognizant Technology Solution
تاريخ النشر: 2014/01/13
Masud Sarkar
من قبل 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.

مستخدم محذوف‎
من قبل مستخدم محذوف‎

try {

}

finally

{}

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

Sandhya Binoy
من قبل 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.

 

Md Khalid Ahmad
من قبل Md Khalid Ahmad , Senior Java Developer , BRITSH TELECOM

Yes we can have.

Cedric Nabaa
من قبل 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?

المزيد من الأسئلة المماثلة

هل تحتاج لمساعدة في كتابة سيرة ذاتية تحتوي على الكلمات الدلالية التي يبحث عنها أصحاب العمل؟