FaultCode.CreateReceiverFaultCode / FaultCode.CreateSenderFaultCode inconsistency - wcf

If FaultCode.CreateReceiverFaultCode(FaultCode subCode) is called with a null argument, an ArgumentNullException is thrown.
If FaultCode.CreateSenderFaultCode(FaultCode subCode) is called with a null argument, no exception is thrown.
The linked MSDN documentation for these methods suggests that both will throw an ArgumentNullException if the subCode argument is null.
What is the reason for this apparently inconsistent behaviour?
The reference source doesn't have any comments that throw light on this.

Related

kotlin singleton exception is bad or good?

i'm studying kotlin.
I have a question about using exception.
which is better to use object exception or class exception in kotlin?
object CustomDuplicateId : RuntimeException("Invalid user with Id")
class CustomDuplicateId : RuntimeException("Invalid user with Id")
if im using this exception only one location, than stacktrace is always same.
so i think... it doesn't need to use class. is it right?
or is it bad to use singleton exception?
can anyone let me know what's better code to write exception?
There are multiple reasons why it's incorrect to use object.
the stacktrace is wrong in several ways:
It's created at instantiation time, so you'll see <clinit> as first line in the stacktrace because it's created when initializing the object's class for the first time
Even if you throw it from the same place, it won't necessarily be when called from the same caller, so the bottom (root) of the stacktrace will be incorrect if this exception is thrown multiple times across the life of your application
technically the exception class has mutable parts and it would be dangerous/incorrect to allow user code to tamper with this. A very concrete example of this is that user code could add suppressed exceptions to your exception instance (which would only be valid for one throw and yet persist in your object). This is technically a memory leak.
it's likely you would need different messages with more information about why it was thrown (in that case the duplicate ID is very much needed) - so you need different instances with different data anyway
you might throw it from different places in the future (even now in tests/mocks maybe?), and in that case the stacktrace would be even more wrong
independently of technicalities like the above, class vs object also sends a message to the reader that is a bit unclear - why an object here? This exception is not inherently unique. It just so happens that you throw it in one specific place right now and rely on the stacktrace being the same.
Here is an example for the major issue (#1):
object MyExceptionObject : RuntimeException("boom")
fun main() {
try {
caller1() // line 7
} catch (e: Exception) {
}
caller2() // line 10
}
fun caller1() = doStuff() // line 13
fun caller2() = doStuff() // line 14
fun doStuff() {
throw MyExceptionObject // line 17
}
The caller1() throws the exception but that one is caught. The caller2() throws the exception again and that one is not caught. The stacktrace we get for the uncaught exception incorrectly shows the caller 1 (with lines 7 and 13) but the correct one should be caller 2 (with lines 10 and 14):
Exception in thread "main" com.example.MyExceptionObject: boom
at com.example.MyExceptionObject.<clinit>(ExceptionObjectExample.kt)
at com.example.ExceptionObjectExampleKt.doStuff(ExceptionObjectExample.kt:17)
at com.example.ExceptionObjectExampleKt.caller1(ExceptionObjectExample.kt:13)
at com.example.ExceptionObjectExampleKt.main(ExceptionObjectExample.kt:7)
at com.example.ExceptionObjectExampleKt.main(ExceptionObjectExample.kt)
All in all, just use a class.

Null conditional operator not working (shows "invoke is not a member of xxx")

I can't seem to find anyone else having this same issue. I have seen that you can use null-conditional operators in VB.NET. E.g.
SendNews?.Invoke("Just in: A newsworthy item...")
However, I'm getting "invoke is not a member of SendNews"
I have tried setting "Option Infer On" but still getting this error.
Any ideas? (using .net 4.6.1)
The null conditional operator is used in place of a null check. For instance, instead of this:
If SendNews IsNot Nothing Then
SendNews.Invoke("...")
End If
You can shorten it to this:
SendNews?.Invoke("...")
But, that's all it does. It does nothing to check if the object actually contains the members being accessed. If you are using late binding (Dim SendNews As Object with Option Strict Off, the rough equivalent of using dynamic in C#), the null conditional operator will not skip the method call if it doesn't exist on the object. It will still throw the same exception as it would have otherwise. Currently, the only way to check if a late-bound object contains a particular method is to catch and ignore the exception or to look for it, by string name, with reflection.

Xamlparse Exception occured

I am getting this error again and again in silver light code .It says, The invocation of the constructor on type "LookupControl.SilverCrmLookup" that matches the specified binding constraints threw an exception. Though there is no error in code .
Check your XAML there must be something wrong in there ..

If an exception pointer is thrown, can EXPECT_THROW in google test capture this exception?

For example, I have a code like:
TEST_F(Testmyexception, testthrownexception)
{
EXPECT_THROW(throw new myexception(), myexception);
}
After compiling and running, it gives an error: Actual: it throws a different type.
Does anynone know the answer?
Thanks,
You're throwing a pointer to a myexception, so you have to expect a pointer in the check:
EXPECT_THROW(throw new myexception(), myexception*);
I think you want:
EXPECT_THROW(throw std::underflow_error(""), std::underflow_error);
BTW the syntax to throw an instance of myexception is:
throw myexception();
the "new" keyword should not be used here.

Proper use of try .. catch

Should I be using this method of throwing errors:
if (isset($this->dbfields[$var])) {
return $this->dbfields[$var];
} else {
throw new FieldNotFoundException($var);
}
or this style:
try {
return $this->dbfields[$var];
} catch (Exception $e) {
throw new FieldNotFoundException($var);
}
...or something else altogether?
quick explanation of the code: $this->dbfields is an array. isset() checks if a variable is set, in this case, whether the array element exists.
The second example is bad. You're taking a lot of overhead to catch an exception when, as you demonstrate, it's just as easy to prevent the exception in the first place. Plus you also assume you know why that exception was thrown - if there was some other exception, like say an out of memory or something, you're reporting it as a "field not found" even if it wasn't.
Keep in mind that try/catch in languages like C++ and Java are very expensive because of all the state they have to save and restore. Python, on the other hand, has very cheap exceptions and they positively encourage you to use a try/except for simple validation. But even so, catching everything and pretending it's one type of exception is still bad.
//First let's do the checks.
if(!isset($this->dbfields[$var]))
throw new FieldNotFoundException($var);
//Now we're in the clear!
return $this->dbfields[$var];
Catching "Exception" is not, most of the time, considered a good practice, out of the two you displayed, I would use option 1.
Catching all exceptions may hide a different exception and mask it as a FileNotFoundException.
I prefer the first one, but if dbfields[$var] throws something reasonable when you access a non-existent element, then I'd prefer just returning it without checking.
I don't particularly like changing the exception type unless I have a good reason -- also if you do, make sure to try to preserve the original exception and stack trace.
Just re-read your explanation.
I guess your method there in #1 is going to catch any exceptions that might be thrown and simply return a bool. I definitely don't like the catching of the generic exception most of the time, so #2 wouldn't be my choice.
"...or something else altogether?"
Neither is very good, so something else would be appropriate.
Fix version 2 to catch the correct exception, not every possible exception. Post that as option 3. I'll upvote something that catches a specific exception instead of Exception.
This is far from language-agnostic.
Some languages won't throw errors for accessing non-existant fields, and the preferred pattern depends a lot on the implementations of the arrays, tables, objects, etc.