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

Exception hierarchy in .NET in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Exception Mastery in .NET
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this code snippet?

Consider the following C# code that catches exceptions:

try {
    throw new System.IO.FileNotFoundException("File missing");
} catch (System.Exception ex) {
    Console.WriteLine(ex.GetType().Name);
}

What will be printed?

C Sharp (C#)
try {
    throw new System.IO.FileNotFoundException("File missing");
} catch (System.Exception ex) {
    Console.WriteLine(ex.GetType().Name);
}
AFileNotFoundException
BSystemException
CException
DIOException
Attempts:
2 left
💡 Hint

FileNotFoundException inherits from IOException, which inherits from Exception.

Predict Output
intermediate
2:00remaining
What exception type is caught here?

Given this code snippet:

try {
    throw new System.ArgumentNullException("param");
} catch (System.SystemException ex) {
    Console.WriteLine(ex.GetType().Name);
}

What will be printed?

C Sharp (C#)
try {
    throw new System.ArgumentNullException("param");
} catch (System.SystemException ex) {
    Console.WriteLine(ex.GetType().Name);
}
ASystemException
BInvalidOperationException
CException
DArgumentNullException
Attempts:
2 left
💡 Hint

ArgumentNullException inherits from SystemException.

Predict Output
advanced
2:00remaining
What error does this code raise?

Examine this code snippet:

try {
    throw new System.Exception("Error");
} catch (System.IO.IOException ex) {
    Console.WriteLine("IO error");
} catch (System.Exception ex) {
    Console.WriteLine("General error");
}

What will be printed?

C Sharp (C#)
try {
    throw new System.Exception("Error");
} catch (System.IO.IOException ex) {
    Console.WriteLine("IO error");
} catch (System.Exception ex) {
    Console.WriteLine("General error");
}
AGeneral error
BIO error
CNo output, program crashes
DCompilation error
Attempts:
2 left
💡 Hint

Exception is not an IOException, but it is an Exception.

Predict Output
advanced
2:00remaining
What is the output of this code with nested exceptions?

Look at this code:

try {
    try {
        throw new System.IndexOutOfRangeException("Index error");
    } catch (System.Exception ex) {
        throw new System.ApplicationException("App error", ex);
    }
} catch (System.ApplicationException ex) {
    Console.WriteLine(ex.InnerException.GetType().Name);
}

What will be printed?

C Sharp (C#)
try {
    try {
        throw new System.IndexOutOfRangeException("Index error");
    } catch (System.Exception ex) {
        throw new System.ApplicationException("App error", ex);
    }
} catch (System.ApplicationException ex) {
    Console.WriteLine(ex.InnerException.GetType().Name);
}
AException
BApplicationException
CIndexOutOfRangeException
DNullReferenceException
Attempts:
2 left
💡 Hint

The inner exception is the original exception thrown inside the inner try block.

🧠 Conceptual
expert
2:00remaining
Which exception type is the base for all CLR exceptions?

In the .NET exception hierarchy, which class is the ultimate base class for all exceptions thrown by the Common Language Runtime (CLR)?

ASystem.ApplicationException
BSystem.Exception
CSystem.SystemException
DSystem.Object
Attempts:
2 left
💡 Hint

Think about the root class that all exceptions inherit from.