Interviews1 answer
The Below Code has pseudo code that performs some file operation . a.Is there a problem with this code ? if YES , What is tht? b. In which block would you write the pseudo code to “close the file “ ? And Why? Class program { Static void main (string []args) { Try {} Catch(Exception ex) { // Block 1 } Catch (File Exception fex) { //Block 2 } Finally {} } }
1 Answer
a.) There is an issue in this code, Block 2 will never be executed, as block 1 catches every exception (all exception classes are derived from Exception). So it should be like below
Try
{}
Catch(FileException ex)
{
// Block 1
}
Catch ( Exception fex)
{
//Block 2
}
b.) In Finally block we would write the pseudo code to “close the file “ because FINALLY block is always executed. It ensures a block of statements are always reached before the enclosing method is exited.
Narasimha Reddy ChennupalliPosted Dec 12, 2014, 8:11 AM EST
