Visual Studio 2019 .NET Core C# Windows Forms development.
SqlDependency.Start is causing a SqlException. But it happens after .Start completes with return code True. So catch does not catch it. Happens periodically. Can see it in VS Output:
Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.SqlClient.dll
Checking SSMS log file:
Service Broker needs to access the master key in the DB. Error 32.
But I see this once. Whereas VS Output shows SqlException in groups of 5 separated by tens of seconds.
In general how can one setup a way to monitor all SqlException that happen outside of a response to a call. Say a time out exception perhaps not that I know if it happens.
In particular suggestions for resolving this are appreciated. I tried setting TRUSTWORTHY to YES but it did not help.
Not sure if SqlException and the log message are related.
Thanks!
That an exception is thrown is not necessarily an issue. It's quite normal for exceptions to be thrown. The VS debugger will show you every exception that is thrown but the vast majority of those are being thrown, caught and dealt with in system code and are most likely of no concern to you.
That said, you can configure VS to break whenever a particular type of exception is thrown, rather than the default of when it goes uncaught. While debugging, open the Exception Settings window, find the exception of interest and check the corresponding box to break whenever that exception gets thrown. It seems that you want the System.Data.SqlClient.SqlException under the Common Language Runtime Exceptions node.
i am searching for that output error recently, but every answer here only explaining why that output happened on a specific case and solved a chunk of code.
like here and here
i hope there's anyone who can give a well elaborated answer, generally explaining what this output is actually mean and how to (elegantly) avoid this error.
note : im working on a project using .NET framework on windows store apps
An ArgumentExeption means that there was an error with an argument that was passed to a function. Normally the exception should contain the name of the offending parameter.
A more specialised exception is the ArgumentNullException which means that an Argument was null where it must not be null.
Or the ArgumentOutOfRangeException which means that an argument requires a specific range (e.g. 1-100) and an invalid value was passed (e.g. 101).
See MSDN for more Information: https://msdn.microsoft.com/de-de/library/system.argumentexception(v=vs.110).aspx?f=255&MSPPError=-2147217396
"A first chance exception" means you've set up your debugger to let you know whenever exceptions get thrown, regardless of whether there's code to handle those exceptions properly. They can even appear inside .NET Framework source code, if you've also set up your debugger to debug all IL code rather than just that of your project.
Unless there's an actual problem, don't worry about first-chance exceptions, just turn off the notifications: in the Exception Settings window, uncheck the "Break When Thrown" checkbox. You'll still be alerted whenever an exception isn't handled.
I have a strange error after catch the error "A generic error occurred in GDI+."
when i try to open an excel file trought the instruction :
Workbook workbook = new Workbook();
workbook.LoadFromFile(FileName);
but few minute after catching this exception my application craches (NullReferenceException )without specifying the location of the error !!
knowing that it happens when i try to scan an excel file, but it work for others excel files !!!
I can't locate where the exception with the message "Object reference not set to an instance of an object" is thrown . Visual Studio show that it can't locate it !!!
So even if i try to handle exceptions in my code i'll have nothing.
Could someone help me in this topic ?
The TL:DR; version is:
Something went wrong when the "Generic Error in GDI+" exception was thrown, which you didn't fix in your exception handler (probably because there was no way for you to do so).
Some time later, something else went wrong as a consequence of the earlier thing.
Long version
You can only handle exceptions which you know how to handle.
To handle an exception, as opposed to merely clean up the local variables and re-throw it, you need to fully understand the following:
Exactly what went wrong
What all the consequences are
Exactly what needs to be done to put everything - all the consequences - right
If you do not know all of those things then you cannot handle the exception correctly.
In your case you have caught an exception (Generic Error in GDI+). However you do not know what caused it, nor how to make sure you have taken care of all the adverse consequences it may have caused.
Once you have had an exception you do not fully understand, it is possible that the internal data structures of the application are no longer consistent. For example things might be missing which are otherwise assumed to be present.
If that's the case, it is to be expected that further problems will arise later on, as they have in your case.
Therefore you shouldn't have caught it, or at least should have re-thrown it. It is time for the process to exit, and page the sysadmin to find out what went wrong. Or at least, to start again with a clean slate.
I'm getting exceptions thrown from somewhere, but all I get from the compiler is "A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll". This is fairly useless to me, as that's not my code (pretty sure it's default library). I'd like to see a stack-trace or something so I know where in my code things went wrong. It's a fairly large codebase (much of which is not mine), and there's a lot of multi-threading and other stuff going on, so it's nearly impossible to try and step through the code without some idea of where to start looking. Is there some setting somewhere to make ALL exceptions trigger a break so I can see the call-stack when they occur, rather than just having them silently fail with a completely useless error message in the output?
You have a couple of options. First, like Greg said, you can cause VS to break when any exception occurs:
Make sure these are checked, then click OK:
That will cause Visual Studio to break wherever the exception occurs.
Another approach is to catch the exception and either write just the stack trace, or write the exception (using ToString()) to the output window:
Then check your output window:
Could you please explain to me what the difference is between an error and an exception?
An exception is a class that takes advantage of language semantics. As others have stated, exceptions interrupt execution up the stack until caught. An exception can be used to convey an error, but more generally is used to convey that something exceptional has occurred.
Errors, on the other hand, can be exceptional or not.
There are several kinds of errors:
User error - this should be handled without an exception
Syntax error - this shouldn't compile in statically typed languages (in dynamic languages, they're a little harder to discover)
Runtime error - this will either result in an exception, or silently fail (usually creating unexpected results)
Really, exceptions should be limited to handling runtime errors, since a user inputting bad data is not "exceptional." To handle user errors, you should take the following approaches:
Prevent bad data from being input (front-end validation)
Prevent bad data from being persisted (back-end validation)
Exceptions should be used as a "last line of defense" for user error. If you're writing a persistence layer, you can rely on exceptions to ensure that bad data that falls through validation does not get persisted. You should, however, fix any of these by putting a fix in the validation that prevents the error from occurring in the first place.
An exception is an object of a type deriving from the System.Exception class. It is used in a throw statement to transfer control to a catch clause in a try block somewhere further up the call stack.
An error is just some code or message that you're meant to interpret. The problem with error codes is that you can decide to ignore them:
MethodThatReturnsAnError();
SomeCodeThatShouldNotExecuteOnError();
That call will simply ignore the error code if one is returned. However:
MethodThatThrowsAnException();
SomeCodeThatShouldNotExecuteOnError();
This cannot be ignored, and will transfer control up the stack, past "SomeCodeThatShouldNotExecuteOnError();".
Exceptions you have to write code to ignore. Error codes you have to write code to not ignore.
Usually, I classify them as:
Error - is a known workflow within the application. For example: Username not provided during authentication is an error.
The application can handle these situation & will be able to show friendly messages to the user to prompt for proper input and/or process the data in a different.
Exception - is usually throw when going out of your system and/or something unexpected happens in the application. For example: opening a file handle might throw an exception due to insufficient rights or the file not existing.
Usually in this case, the application can catch these exception and/or write a generic handler to handle all the exceptions in the system.
As a rule of thumb, if you know a particular case exists due to which the application cannot proceed working, label it as an error & handle the case gracefully.
All remaining 'unknown-unknows' can then fall into the category of Exceptions.
HTH.
If no exception handler for a given exception is present, the program stops executing with an error message.
Unhandled Exceptions are Errors. So All Errors are Exceptions but the reverse is not true.
Exception Handlings technique handles the Exception/Unexpected Situations(Errors), While errors are Situations which we havenot expected to occur so explicitly we have to take care of them by redirectly the User(s) to some static HTML Page & capturing it into Logs & come up with a Solution for the Error occured.
Errors Can Occur at 2 Levels
Page Level( use ErrorPage Attribute at the page Directive)
Application Level(Need to be Handled in web.config)
Compilation
CustomError ... CustomError
error.... error
Compilation
Note- Page Level Error handling overrides the Application Level Error Handling.
Exception Handling:->
Locally (Method Level)
Page Level
Aplication Level
Http module Level.
Will
Link-> http://msdn.microsoft.com/en-us/library/ms173160(v=vs.80).aspx
Exceptions are a way of reporting and handling execution failures. In other words, they are for communicating error conditions (paraphrasing Krzysztof Cwalina in the book Framework Design Guidelines).
Exception: When a step in some action fails, all the subsequent steps in that action are simply NOT executed. This is where exceptions shine.
Error: is when, like in the first case you want to halt execution of the current code, but before you do you need to free any resources previously allocated.
Having that said,
Exception class has HResult property. HRESULT is a 32-bit value, divided into three different fields: a severity code, a facility code, and an error code.
Have a look at this post, will help you understand better
Error codes or Exceptions?
Errors are events. Exception class represents errors that occur during application execution (runtime) and provides a mechanism to handle them using try catch blocks.
Errors could be runtime or compiler error/s.
Examples of error events:
HttpApplication.Error Event of System.Web dll, FileSystemWatcher.Error Event of System.IO
Both dlls have same definition of Error event
public event EventHandler Error
From .Net Framework 4.5 documentation https://msdn.microsoft.com/en-us/library/system.exception(v=vs.110).aspx
Exception class represents errors that occur during application execution.
Errors and exceptions
Run-time errors can occur for a variety of reasons. However, not all errors should be handled as exceptions in your code. Here are some categories of errors that can occur at run time and the appropriate ways to respond to them.
Usage errors. A usage error represents an error in program logic that can result in an exception. However, the error should be addressed not through exception handling but by modifying the faulty code.
Program errors. A program error is a run-time error that cannot necessarily be avoided by writing bug-free code.
In some cases, a program error may reflect an expected or routine error condition. In this case, you may want to avoid using exception handling to deal with the program error and instead retry the operation.
In other cases, a program error reflects an unexpected error condition that can be handled in your code.
System failures. A system failure is a run-time error that cannot be handled programmatically in a meaningful way. For example, any method can throw an OutOfMemoryException exception if the common language runtime is unable to allocate additional memory. Ordinarily, system failures are not handled by using exception handling. Instead, you may be able to use an event such as AppDomain.UnhandledException and call the Environment.FailFast method to log exception information and notify the user of the failure before the application terminates.