What will
happen if you put System.exit(0) on try or catch block?
In normal Finally block will always execute. The only case
finally block is not executed is System.exit(0).
In advanced case it will execute in following case.
By Calling
System.exit(0) in try or catch block, its stops execution & throws SecurityException.
·
If System.exit(0) NOT throws security exception, then finally block Won’t be executed
·
But, if System.exit(0)
throws security exception then finally block will be executed.
java.lang.System.exit() will
terminates the currently executing program by JVM.
·
exit(0) : Generally used to indicate
successful termination.
·
exit(1) or exit(-1) or any other non-zero
value –indicates unsuccessful termination.
What happens if
we put return statement on try/catch? Will finally block execute?
Yes, finally
block will execute even if you put a return statement in the try block or catch
block.
try {
//try block
...
return
success;
}
catch (Exception ex) {
//catch block
.....
return
failure;
}
finally {
System.out.println("Inside finally");
}
The answer is
yes. finally block will execute. The only case where it
will not execute is when it encounters System.exit().
What happens
when a finally block has a return statement?
Finally block overrides the value returned
by try and catch blocks.
public static int myTestingFuncn(){
try{
....
return 5;
}
finally {
....
return 19;
}
}
This program would
return value 19 since the value returned by try has
been overridden by finally.
Why do you think Checked Exception exists in Java, since
we can also convey error using RuntimeException?
Most of checked exceptions are in java.io package, which make sense because if you request any system resource and its not available, than a robust program must be able to handle that situation gracefully.
Most of checked exceptions are in java.io package, which make sense because if you request any system resource and its not available, than a robust program must be able to handle that situation gracefully.
By declaring IOException as
checked Exception, Java ensures that your provide that gracefully exception
handling. Another possible reason could be to ensuring that system resources
like file descriptors, which are limited in numbers, should be released as soon
as you are done with that using catch or finally block
Have you faced OutOfMemoryError in Java? How did you
solved that?
OutOfMemoryError in Java is a subclass of java.lang.VirtualMachineError and JVM throws java.lang.OutOfMemoryError when it ran out of memory in the heap.
OutOfMemoryError in Java is a subclass of java.lang.VirtualMachineError and JVM throws java.lang.OutOfMemoryError when it ran out of memory in the heap.
An easy way to solve OutOfMemoryError in java is to increase the maximum heap size by using JVM options
"-Xmx512M", this will immediately solve your OutOfMemoryError.

0 comments:
Post a Comment