Register now or log in to join your professional community.
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.....
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.
Yes we can have.
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?