exception-handling interview questions
Top exception-handling frequently asked interview questions
I saw this tip in another question and was wondering if someone could explain to me how on earth this works?
try { return x; } finally { x = null; }
I mean, does the finally
clause really execute after the return
statement? How thread-unsafe is this code? Can you think of any additional hackery that can be done w.r.t. this try-finally
hack?
Source: (StackOverflow)
Can I define custom types for user-defined exceptions in JavaScript? If I can, how would I do it?
Source: (StackOverflow)
I often see comments on other Stack Overflow questions about how the use of except: pass
is discouraged. Why is this bad? Sometimes I just don't care what the errors, are and I want to just continue with the code.
try:
something
except:
pass
Why is using an except: pass
block bad? What makes it bad? Is it the fact that I pass
on an error or that I except
any error?
Source: (StackOverflow)
I've seen a lot of posts about stack trace and exceptions in Python. But haven't found what I need.
I have a chunk of Python 2.7 code that may raise an exception. I would like to catch it and assign to a string its full description and the stack trace that caused the error (simply all we use to see on the console). I need this string to print it to a text box in the GUI.
Something like this:
try:
method_that_can_raise_an_exception(params)
except Exception, e:
print_to_textbox(complete_exception_description(e))
The problem is: what is the function complete_exception_description
?
Source: (StackOverflow)
var connection = ConnectionFactory.GetConnection(ConfigurationManager.ConnectionStrings["Test"].ConnectionString, DataBaseProvider);
And this is my App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="Test" connectionString="Data Source=.;Initial Catalog=OmidPayamak;IntegratedSecurity=True" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
But when my project runs this is my error:
Object reference not set to an instance of an object.
Source: (StackOverflow)
What are the best practices to consider when catching exceptions and re-throwing them? I want to make sure that the Exception object's InnerException and stack trace are preserved. Is there a difference between the following code blocks in how they handle this?
try
{
//some code
}
catch (Exception ex)
{
throw ex;
}
//......
try
{
//some code
}
catch
{
throw;
}
Source: (StackOverflow)
Is it possible to do the following using ELMAH:
logger.Log(" something");
I'm doing something like this:
try
{
// Code that might throw an exception
}
catch(Exception ex)
{
// I need to log error here...
}
This exception will not be automatically logged by ELMAH because it was handled.
Source: (StackOverflow)
I'd like to do something like this:
some_method.should_raise <any kind of exception, I don't care>
How should I do this?
some_method.should_raise exception
... doesn't work.
Source: (StackOverflow)
sometimes, under not reproducible circumstances, my WPF application crashes without any message. The application simply close instantly.
Where is the best place to implement the global Try/Catch block. At least i have to implement a messagebox with: "Sorry for the inconvenience ..."
Source: (StackOverflow)
There are some posts that asks what the difference between those two are already.
(why do I have to even mention this...)
But my question is different in a way that I am calling "throw ex" in another error god-like handling method.
public class Program
{
public static void Main(string[] args)
{
try
{
// something
}
catch (Exception ex)
{
HandleException(ex);
}
}
private static void HandleException(Exception ex)
{
if (ex is ThreadAbortException)
{
// ignore then,
return;
}
if (ex is ArgumentOutOfRangeException)
{
// Log then,
throw ex;
}
if (ex is InvalidOperationException)
{
// Show message then,
throw ex;
}
// and so on.
}
}
If try & catch
were used in the Main
, then I would use throw;
to rethrow the error.
But in the above simplied code, all exceptions go through HandleException
Does throw ex;
has the same effect as calling throw
when called inside HandleException
?
Source: (StackOverflow)
The noexcept
keyword can be appropriately applied to many function signatures, but I am unsure as to when I should consider using it in practice. Based on what I have read so far, the last-minute addition of noexcept
seems to address some important issues that arise when move constructors throw. However, I am still unable to provide satisfactory answers some practical questions that led me to read more about noexcept
in the first place.
There are many examples of functions that I know will never throw, but for which the compiler cannot determine so on its own. Should I append noexcept
to the function declaration in all such cases?
Having to think about whether or not I need to append noexcept
after every function declaration would greatly reduce programmer productivity (and frankly, would be a pain). For which situations should I be more careful about the use of noexcept
, and for which situations can I get away with the implied noexcept(false)
?
When can I realistically expect to observe a performance improvement after using noexcept
? In particular, give an example of code for which a C++ compiler is able to generate better machine code after the addition of noexcept
.
Personally, I care about noexcept
because of the increased freedom provided to the compiler to safely apply certain kinds of optimizations. Do modern compilers take advantage of noexcept
in this way? If not, can I expect some of them to do so in the near future?
Source: (StackOverflow)