0
0
C Sharp (C#)programming~20 mins

Custom exception classes in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom Exception Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of throwing and catching a custom exception

What will be the output of this C# program when it throws and catches a custom exception?

C Sharp (C#)
using System;

class MyCustomException : Exception {
    public MyCustomException(string message) : base(message) {}
}

class Program {
    static void Main() {
        try {
            throw new MyCustomException("Oops, something went wrong!");
        } catch (MyCustomException ex) {
            Console.WriteLine(ex.Message);
        }
    }
}
AMyCustomException
BNo output, program crashes
CSystem.Exception: Oops, something went wrong!
DOops, something went wrong!
Attempts:
2 left
💡 Hint

Look at what ex.Message prints when caught.

🧠 Conceptual
intermediate
1:30remaining
Purpose of custom exception classes

Why do programmers create custom exception classes instead of using only built-in exceptions?

ATo provide more specific error information related to their application domain
BTo automatically fix errors without user intervention
CBecause built-in exceptions cannot be caught in try-catch blocks
DTo make the program run faster by avoiding built-in exceptions
Attempts:
2 left
💡 Hint

Think about how custom exceptions help in understanding errors better.

🔧 Debug
advanced
2:00remaining
Identify the error in this custom exception class

What error will this code produce when compiled?

C Sharp (C#)
using System;

class MyException : Exception {
    public MyException(string message) {
        base.Message = message;
    }
}

class Program {
    static void Main() {
        throw new MyException("Error occurred");
    }
}
ACompile-time error: Cannot assign to 'Message' because it is read-only
BRuntime error: NullReferenceException
CNo error, prints 'Error occurred'
DCompile-time error: Missing base constructor call
Attempts:
2 left
💡 Hint

Check if Message property can be assigned directly.

📝 Syntax
advanced
2:30remaining
Correct syntax to define a custom exception with serialization support

Which option correctly defines a custom exception class that supports serialization?

A
using System;
using System.Runtime.Serialization;

class MyException : Exception {
    public MyException() {}
    public MyException(string message) : base(message) {}
    protected MyException(SerializationInfo info, StreamingContext context) : base(info, context) {}
}
B
using System;

class MyException : Exception {
    public MyException() {}
    public MyException(string message) : base(message) {}
    protected MyException(SerializationInfo info, StreamingContext context) {}
}
C
using System;
using System.Runtime.Serialization;

[Serializable]
class MyException : Exception {
    public MyException() {}
    public MyException(string message) : base(message) {}
    protected MyException(SerializationInfo info, StreamingContext context) : base(info, context) {}
}
D
using System;
using System.Runtime.Serialization;

[Serializable]
class MyException {
    public MyException() {}
    public MyException(string message) {}
}
Attempts:
2 left
💡 Hint

Serialization requires the [Serializable] attribute and a protected constructor calling base.

🚀 Application
expert
3:00remaining
Determine the number of caught exceptions

Given this code, how many exceptions will be caught and printed?

C Sharp (C#)
using System;

class CustomEx : Exception {}

class Program {
    static void Main() {
        int count = 0;
        try {
            throw new CustomEx();
        } catch (Exception) {
            count++;
            try {
                throw new CustomEx();
            } catch (CustomEx) {
                count++;
            }
        }
        Console.WriteLine(count);
    }
}
A1
B2
C0
D3
Attempts:
2 left
💡 Hint

Count increments each time an exception is caught.