0% found this document useful (0 votes)
18 views3 pages

Compiler Rules

Your description should be a few sentences long.

Uploaded by

urgesa100
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views3 pages

Compiler Rules

Your description should be a few sentences long.

Uploaded by

urgesa100
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Compiler rules & common errors (what

causes compile-time errors)


 Order of catches: more specific → more general. catch (Exception) (or catch
{ }) must be last. If you put catch (Exception) before a specific catch, the later
one is unreachable and the compiler errors.
 Duplicate catches: You cannot have two catches with the same type (e.g., two catch
(FormatException) blocks).
 Parameterless catch & catch (Exception ex): these are equivalent; don’t use
both — the parameterless one must be last if present.
 Multiple finally blocks: Not allowed — each try can have at most one finally.
 throw types: you must throw an object deriving from System.Exception.

Corrected + idiomatic examples (fixes to the


examples you posted)
1) Divide 100 example — specific catches, correct parsing:
using System;

class Program
{
static void Main()
{
Console.Write("Please enter a number to divide 100: ");
try
{
int num = int.Parse(Console.ReadLine()); // may throw
FormatException or ArgumentNullException
int result = 100 / num; // may throw
DivideByZeroException
Console.WriteLine("100 / {0} = {1}", num, result);
}
catch (DivideByZeroException)
{
Console.WriteLine("Cannot divide by zero. Please try again.");
}
catch (FormatException)
{
Console.WriteLine("Not a valid format. Please enter a numeric
value.");
}
catch (Exception ex)
{
Console.WriteLine("Error occurred: " + ex.Message);
}
}
}
1. Which C# keyword is used to rethrow the current exception preserving its original
stack trace?
A. throw ex; B. throw; C. throw new Exception(); D. rethrow
Answer: B (throw;) — throw; rethrows the active exception without resetting the
stack trace.
2. Which catch block will be reached first for a thrown FormatException if both are
present?
A. catch (Exception) then catch (FormatException)
B. catch (FormatException) then catch (Exception)
C. Order does not matter
D. Compiler error
Answer: B — specific handlers must come before general; runtime picks first
matching catch.
3. Which statement about a parameterless catch { } is true?
A. It is different from catch (Exception ex) in behavior.
B. It must be first.
C. It is equivalent to catch (Exception) and must be last if present.
D. It only catches fatal exceptions.
Answer: C — parameterless catch equals catch (Exception) and must be last.
4. Exception filters in C# use which keyword?
A. where B. filter C. when D. if
Answer: C (when) — catch (Exception ex) when (condition).
5. What happens when you throw ex; inside a catch block?
A. Preserves original throw location.
B. Resets stack trace to the throw ex; line.
C. Converts to AggregateException.
D. Suppresses the exception.
Answer: B — throw ex; resets the exception's stack trace to the current throw.
6. Which construct is guaranteed to run during stack unwinding (except catastrophic
termination)?
A. catch B. finally C. using D. throw
Answer: B (finally) — finally executes during unwind for cleanup.
7. Where should catch (Exception) generally appear?
A. First catch block
B. Last catch block (or top-level boundary)
C. Anywhere — order is irrelevant
D. Only inside finally
Answer: B — general catch must be last.
8. Which of these is true about multiple finally blocks for one try?
A. Allowed anywhere
B. Allowed only inside nested try
C. Not allowed — only one finally per try
D. Compiler automatically merges them
Answer: C — only one finally per try.
9. Use using for objects that implement which interface?
A. IAsyncResult B. IDisposable C. ICloneable D. IFormattable
Answer: B (IDisposable) — using ensures Dispose() is called.
10. Which is the correct effect of an exception filter (when) when the condition is false?
A. The exception is swallowed.
B. The filter is ignored and next handler is evaluated; stack trace unchanged.
C. The filter throws another exception.
D. The program terminates.
Answer: B — if when is false, runtime searches for another handler.
11. Which is the best practice for rethrowing an exception with added context?
A. throw ex; after adding message
B. throw new Exception("context");
C. throw new MyException("context", ex);
D. throw; then modify ex.Message
Answer: C — wrap original as InnerException to preserve cause and add context.
12. A catch block with a more general exception type placed before a specific one will
cause:
A. Runtime to skip the general and pick specific.
B. Compile-time error (unreachable catch).
C. Program to crash at runtime.
D. Both catches executed sequentially.
Answer: B — specific later is unreachable; compiler error.
13. Which is the recommended way to validate numeric user input?
A. int.Parse() inside try/catch
B. Convert.ToInt32() inside try/catch
C. int.TryParse() without exceptions
D. Use regex and then int.Parse()
Answer: C — TryParse avoids exceptions for expected invalid input.

What happens to local variables when stack unwinds?


A. They remain valid and accessible.
B. They go out of scope; their lifetimes end.

You might also like