Senator Guerra Souty original series calendar,replica hublot blue steel peach pointer collocation of rolex replica Rome digital scale, track type minute replica watches scale shows that the classical model is swiss replica watches incomparable, wearing elegant dress highlights.
mr-ponna.com

 

YOU ARE HERE: HOME Questions Error handling in Try catch statement try b 0 c a b catch Exception ex catch related exception



About Error handling and Try catch finally statements

View(s): 38935

Error handling in Try catch statement,try{b=0; c=a/b;}catch (Exception ex){}catch //related exception{}finally{}Which exception will catch first and what about finally statement?

Answer 1)

Refactored question for readability

Q) Which exception will catch first and what about finally statement?

try

{

    int b = 0;

    int c = 10 / b;

}

catch (Exception e)

{

}

catch (DivideByZeroException ae)

{

}

finally

{

}


Ans)
Multiple catch blocks are evaluated from top to bottom, but only one catch block is executed for each exception thrown. The order of your catch blocks is important because .NET will go to the first catch block that is polymorphically compatible with the thrown exception. It is important to place catch blocks with the most specific — most derived — exception classes first.

In your example, the 1st catch is type of Exception of which is base class for all exceptions and thereby polymorphically compatible with all thrown exceptions. so 2nd catch never can get execute.

And regarding finally block, it always executes after the try and catch blocks execute. A finally block is always executed, regardless of whether an exception is thrown or whether a catch block matching the exception type is found.

  Asked in:  iGATE   Expertise Level:  Intermediate
  Last updated on Sunday, 04 May 2014
4/5 stars (26 vote(s))

Register Login Ask Us Write to Us Help