Example & Tutorial understanding programming in easy ways.

Can we have a try block without catch or finally block?


No you can't

The reason why you cannot have a finally without a try is because you could have multiple finally statements in the same scope and the try indicates what block of code the finally pertains to in case an error occurs.

Another interesting feature of the finally is that it must execute no matter what when the try is entered. For example what if you use a goto to skip over your finally statement? If the goto is inside of the try it will execute the finally statement however if the goto is above/outside the try statement then it will skip the finally code. finally is relevant only to the code that is surrounded in the try. If you have no try then the finally is not relevant to anything.

you can use Try-catch-finally or try-finally

 try {

    }catch (Exception e){

    }
    finally{

    }

or

try {

    }
    finally{

    }

Read More →