C# Exception Hierarchy Quiz
29 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

Which type of exception is generated by user programs?

  • ApplicationException (correct)
  • SystemException
  • FormatException
  • OutOfMemoryException

What exception is thrown when an array index is out of bounds?

  • IndexOutOfRangeException (correct)
  • StackOverflowException
  • OverflowException
  • ArgumentException

What is the purpose of the 'catch' keyword in C# exception handling?

  • To execute a block of code regardless of exceptions
  • To manually throw an exception
  • To catch exceptions that occur in the try block (correct)
  • To check for exceptions in the program

What keyword is used to ensure a block of code executes regardless of exceptions?

<p>finally (C)</p> Signup and view all the answers

Which exception is thrown on arithmetic errors like division by zero?

<p>ArithmeticException (C)</p> Signup and view all the answers

What happens when there is insufficient memory for a memory allocation request?

<p>OutOfMemoryException (B)</p> Signup and view all the answers

When an unacceptable argument is passed to a method, which exception is thrown?

<p>ArgumentNullException (B)</p> Signup and view all the answers

In exception handling, which keyword allows you to throw an exception manually?

<p>throw (C)</p> Signup and view all the answers

What exception is explicitly thrown when either num1 or num2 is zero?

<p>DivideByZeroException (C)</p> Signup and view all the answers

What will happen if the user enters a non-numeric value in txtNum1 or txtNum2?

<p>A FormatException will be caught. (C)</p> Signup and view all the answers

How does the GetQuotient method handle division?

<p>It performs integer division and returns an integer. (C)</p> Signup and view all the answers

Which keyword is used to manually throw an exception in this code?

<p>throw (D)</p> Signup and view all the answers

What must be done to create a custom exception in .NET Framework?

<p>Inherit from the System.Exception class or its derived classes. (C)</p> Signup and view all the answers

What keyword is used to manually throw an exception?

<p>throw (D)</p> Signup and view all the answers

What happens if an exception is thrown without a try-catch block?

<p>An error message is displayed, and the application closes. (D)</p> Signup and view all the answers

Which object is created when throwing an exception manually?

<p>exception_Object (B)</p> Signup and view all the answers

What exception is specifically thrown in the provided code when num1 or num2 is 0?

<p>DivisionByZeroException (D)</p> Signup and view all the answers

Which block is used to handle exceptions in code?

<p>catch block (A)</p> Signup and view all the answers

What will the method GetQuotient return if called with parameters 10 and 2?

<p>5 (D)</p> Signup and view all the answers

What is indicated by the use of the new operator in the context of exception handling?

<p>It creates a new exception object. (B)</p> Signup and view all the answers

Which of the following statements is true regarding the finally block?

<p>It executes regardless of whether an exception is thrown. (D)</p> Signup and view all the answers

What is the purpose of a custom exception class in programming?

<p>To handle errors specific to application logic. (A)</p> Signup and view all the answers

When creating a custom exception class, which base class should it derive from?

<p>Exception (C)</p> Signup and view all the answers

In the given code, what does the base constructor of the custom exception class do?

<p>It passes the error message to the base Exception class. (B)</p> Signup and view all the answers

What specific string is thrown by the InvalidUserInputException class when the input age is not legal?

<p>Not in legal age! (C)</p> Signup and view all the answers

In the checkAge method, which condition will trigger the InvalidUserInputException?

<p>Both A and C. (B)</p> Signup and view all the answers

How does the application respond when a valid age is provided in the btnCheck_Click event?

<p>A message box shows 'Legal Age!'. (C)</p> Signup and view all the answers

What happens if the checkAge method doesn't throw an exception?

<p>The 'Legal Age!' message will be displayed. (C)</p> Signup and view all the answers

Which option correctly describes the purpose of the InvalidUserInputException class?

<p>To handle user input errors related to age. (D)</p> Signup and view all the answers

Study Notes

Exception Hierarchy

  • All exceptions in C# are subclasses of the Exception class, which is part of the System namespace.
  • Two main types of exceptions:
    • ApplicationException: User-defined exceptions.
    • SystemException: Exceptions generated by the Common Language Runtime (CLR).

Common Exceptions

  • System.Exception: The base class for all exceptions.
  • System.ArithmeticException: Thrown for errors in arithmetic operations or conversions.
  • System.OverflowException: Thrown when an overflow occurs in a checked operation.
  • System.ArgumentException: Thrown when a method receives an invalid argument.
  • System.ArgumentNullException: Thrown when a method receives a null argument.
  • System.IndexOutOfRangeException: Thrown when an array is accessed with an index that is outside its bounds.
  • System.OutOfMemoryException: Thrown when there is insufficient memory to allocate a request.
  • System.StackOverflowException: Thrown when the execution stack is exhausted due to too many pending method calls.
  • System.FormatException: Thrown when a string or argument does not have the expected format.

The Try-Catch Block

  • Used to handle exceptions in C#.
  • Four keywords are used:
    • try: Contains the block that may throw an exception.
    • catch: Catches the exception thrown in the try block.
    • throw: Manually throws an exception.
    • finally: Executes a given statement regardless of whether an exception is thrown or not.

Throwing an Exception

  • Can be done automatically by the runtime system or manually using the throw keyword.
  • Syntax of manually throwing an exception: throw new exception_Object;
  • exception_Object is an instance of a class derived from the Exception class.

Creating Custom Exceptions

  • C# allows creating custom exceptions by inheriting from the Exception class or one of its derived classes.
  • A custom exception class is created using the following format:
    public class CustomizeException: Exception{
        // code here
    }
    
  • A constructor is added with the following format:
    public class CustomizeException: Exception{
        public CustomException(string str): base(str){
        }
    }
    
  • The CustomizeException class now acts like other standard exceptions and can be thrown and caught.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

03_Handout_1.pdf

Description

Test your knowledge of exceptions in C# programming! This quiz covers the different types of exceptions, including application and system exceptions, as well as common examples such as ArgumentNullException and StackOverflowException. Perfect for anyone looking to deepen their understanding of error handling in C#.

More Like This

Handling Exceptions in C# Programming
27 questions
Error Handling and Exceptions in Programming
21 questions
Java Exceptions and Error Handling
24 questions
Use Quizgecko on...
Browser
Browser