Click here to Skip to main content
15,910,471 members
Please Sign up or sign in to vote.
2.23/5 (4 votes)
See more:
Please help me with the below program in c#.

C#
static void Main(string[] args)
{
int a, b;
a= 100; b=0; 
try
{
   Console.WriteLine("before");
   Console.Write(a/b);
   Console.WriteLine("after");
}
catch (Exception ex)
{
   Console.WriteLine("Exception");
}
catch (DivideByZeroException ex)
{
   Console.WriteLine("DivideByZeroException");
}
finally
{
   Console.WriteLine("Finally");
}
   Console.WriteLine("End";);
}


a)There is a structure flaw on the code. Can you explain it and how to correct it?
b)After correcting, which text would be shown after execution? Justify

Thanks :)

additional information moved copied from comment below
I already tried it myself. I couldn't figure out the structural flaw..but the text shown for execution I think it should be before,dividebyzeroexception,finally and end. Please let me know if my understanding is correct. I am new to c# programming. Though I guess an answer I am doubtful to post it in the public form.
Posted
Updated 27-Dec-13 14:13pm
v6

You have a syntax error here:
Console.WriteLine("End";);
The extra semi-colon will prevent compiling.

The structural error is that your first catch of the exception is going to catch all errors, and handle them, so the compiler "doesn't know what to do" with the second catch, and will cause this error: "A previous catch clause already catches all exceptions of this or of a super type ('System.Exception')"

If you put the more specific catch clause for a divide-by-zero error before the catch-everything catch clause, this will work:
C#
catch (DivideByZeroException ex)
{
    Console.WriteLine("DivideByZeroException");
}
catch (Exception ex)
{
    Console.WriteLine("Exception");
}
 
Share this answer
 
Comments
Member 10491035 27-Dec-13 18:27pm    
Does that mean whenever I am writing the catch exceptions I should write the specific expected exceptions first and the generic one (i.e Exception ex ) at the end? Now if the code is modified as above then the statements printed on console are before,dividebyzeroexception,finally and end. Is that correct?
BillWoodruff 27-Dec-13 18:31pm    
Yes. I find it valuable, any time I am writing try/catch/finally clauses, to kind of mutter the mantra "specific to general" :)

What is written to the Console ? Run the program, and examine the output.

It's also a very good idea to get in the habit of using your IDE's debugger: set break-points; and, single-step through your code, observe the flow-of-control, examine the values of variable.

good luck, Bill
Member 10491035 27-Dec-13 18:57pm    
If an exception is thrown while in the try block then the remainder of the try block will not be executed and the catch block will start executing. Once the catch block completes then the finally block gets executed and normal execution resumes with the code below the finally block.
So the text printed is
Before
DivideByZeroException
Finally
End

Is my above justification correct?
Nelek 27-Dec-13 19:06pm    
In this case yes. That is the importance of the "try, catch, finally" block.
With them, you can get a "bypass" to an error and make something to fight against, notify it or other actions.
Without it, you would get a runtime problem and your program would be probably aborted.

On the other hand... they are not the "panacea". I mean:
If your example asked for the numbers to the user, I would prefer to validate the user input for the divisor, instead of using the catch block to say "you divided by zero". Avoiding the exception is (at least in my opinion) better thatn handling it
BillWoodruff 27-Dec-13 19:35pm    
In teaching C# programming, I've found it valuable to ask students to think of try/catch/finally as (kind-of) meaning:

// Try: Try This ...

// Catch 1: If it failed like this: ... Type of Specific Error

// Catch 2: If it failed like this: ... Type of Less Specific Error

// Catch 3: If it failed for any other reason (general Exception)

// Finally: Do this no matter what
This is not how CP usually works. Most important goal here is to learn and help learning.
You are supposed to try it on your own, and come here when you got stuck with something, with a concrete question about your code, design, etc.
Please have a look to What have you tried?[^] to see a good explanation about what I mean.
Don't forget people here don't get payed. And besides, if we give you a ready-to-go solution, it is not going to help you because you are not going to learn anything from it.

In addition, we don't do Homework[^]

You can try it giving a guess and an explanation of it, then ask something and you will get your answers... What you posted means: "please do my work for me"
 
Share this answer
 
Comments
Member 10491035 27-Dec-13 18:12pm    
Hi nelek,

I already tried it myself. I couldn't figure out the structural flaw..but the text shown for execution I think it should be before,dividebyzeroexception,finally and end. Please let me know if my understanding is correct. I am new to c# programming. Though I guess an answer I am doubtful to post it in the public form.
Nelek 27-Dec-13 18:50pm    
As I answered in the comment below. I am sorry if you felt bad about my answer, but I repeat the validity of the links I posted in the solution. If you want to avoid answers like mine, please try to transmit your previous effort: Explaining what you tried, what you think but you are not sure... as you correctly did in the comment I am now answering.
Then... you will not get this kind of answers.
BillWoodruff 27-Dec-13 18:27pm    
Nelek, how about being more careful before judging a post of someone relatively new to CodeProject ? In this case, the OP provided code, and his language is appropriate, and respectful.

I believe that responses such as yours, posted as "solutions," are inappropriate. Commentary on the quality of the question, or its "fit" on CodeProject, are not answers, and should be entered as comments on the OP.
Nelek 27-Dec-13 18:46pm    
I said nothing about his language. I said nothing about being not appropiate or not respectful. He just provided some code and two requirements, and don't tell me it doesn't smell as just copied from the excercise book.
Instead of writing the question as a requirement // copy of the class book, he should have posted what he posted in the comment to my "solution", then the answer would have been very different.

You think I met the wrong "question" to post my "solution", ok. I am not afraid to apologize if I make a mistake.

You can check it if you want, my first option is most of the times the comment (almot eight times more comments than answers). But I sometimes post this "solution" to remove the "question" from the unsanswered list.
BillWoodruff 27-Dec-13 19:26pm    
"don't tell me it doesn't smell as just copied from the excercise book." Do you have any evidence ... other than your "nose" ... to indicate this is copied ? If so, flag the OP as abuse.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900