I have a .net core web api application. So here if I get a null exception and it throws response as shown below. So here in visual studio i get option to click on continue and once i click on continue then only it executes the further program. But when i host this application it will automatically take care and continue the execution of program? is this called throwing of exception?
Asked
Active
Viewed 721 times
0
-
In your code, you should check, if the variable is null and then do something else, if it is. Otherwise the request will will fail with a '500 Server error'. – Poul Bak Sep 29 '20 at 08:36
-
5You're asking the wrong question. You should be asking how to fix this instead of bypassing it. A NullReferenceException typically appears due to coding bugs and shouldn't be ignored. You have no idea what your program is doing at that point – Panagiotis Kanavos Sep 29 '20 at 08:45
-
_"You should be asking how to fix this instead of bypassing it"_ - which would probably be dupe-closed ... I think this is more of a question about understanding exception handling in a more general sense. – Fildor Sep 29 '20 at 08:47
-
In fact, the popup tells you what's wrong: `portfolioCompanies` is null. Why is it null? Did you forget to initialize it? Was it supposed to contain an empty list and got a `null` instead? If your code was supposed to insert those companies into a table, how can your application ignore this error and end up with incorrect or partial data in the database? – Panagiotis Kanavos Sep 29 '20 at 08:47
-
"is this called throwing of exception"...yes. "when i host this application it will automatically take care and continue the execution of program"...no, it will crash and stop. Therefore you need to fix the bug so you can prevent this ever happening. (Note: you _can_ use a try/catch block to catch exceptions and prevent them from stopping the application, but you should only generally do this as a) a way to trap the exception for logging purposes, or b) when the throwing of the exception can be expected sometimes and is out of your control to fix - e.g. from an external library perhaps) – ADyson Sep 29 '20 at 08:49
-
I got a bug report about monthly portfolio reports with -100% performance. Turns out someone years ago had stored `-1` as the EUR-USD exchange rate during a migration, the code failed and *ignored* the error, returning 0 as the XR. So the monthly reports ended up with $0 for those particular stocks. – Panagiotis Kanavos Sep 29 '20 at 08:50
-
@PanagiotisKanavos _"someone years ago"_ - and **nobody** noticed? Oh, the rage... – Fildor Sep 29 '20 at 08:52
-
3You should check [What is a NullReferenceException and how to fix it](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it). Your site won't crash, it will return a 500 error to the caller and keep processing requests. The application's state may be corrupted though, or incomplete data may end up in the database – Panagiotis Kanavos Sep 29 '20 at 08:53
-
@Fildor well, the monthly reports run *client-side* and were so slow they eventually timed out. In most cases, there was enough data to generate the monthly reports. The error was discovered only when moved the report generation to the server and used proper SQL and indexing to make it run in seconds. – Panagiotis Kanavos Sep 29 '20 at 08:55
-
2@Fildor why so surprised? Remember how Lloyd's and other major UK banks crashed in January due to hacky Y2K 'fixes" that took `20` to mean `1920`? – Panagiotis Kanavos Sep 29 '20 at 08:57
-
2@Fildor [Debugging with a Fishbone](https://www.dotnetzone.gr/cs/blogs/pkanavos/archive/2006/12/04/21672.aspx) is based on a different bug hunt in the same company. After discovering 5-6 WT? bugs, it turned out the customer was looking at the wrong folder – Panagiotis Kanavos Sep 29 '20 at 09:00
-
@PanagiotisKanavos It's more disappointment, than surprise. We just found out one of our app's performance indicators "that's oh so important" was computed completely incorrectly and inconsistently on top. We corrected it and nobody cared or even noticed... so disappointing. – Fildor Sep 29 '20 at 09:03
-
@ADyson it is expected to be null and a background process is running behind. Once that gets completed portfoliocompany will have data. But here i dont want to wait till background process is completed. I want it to be executed normally – Ekta Sep 29 '20 at 09:07
-
3If it is "expected" to be null, then a NRE should not be thrown. So, there is in fact at least _one_ peace of code, where you neglected that expectation. – Fildor Sep 29 '20 at 09:23
-
1@Ekta if it's expected to be null, check the variable for null and avoid that exception. Throwing an exception is orders of magnitude slower than a simple check. If you don't want to wait for a background process though, cancel it first. Add a timeout. There are several ways to handle your original problem, whatever it is. Allowing your code to throw isn't one of them – Panagiotis Kanavos Sep 29 '20 at 09:50
-
@Ekta, As we all known, the issue is related the portfolioCompanies is null. So, try to initialize it, and check whether this value is null or not before using it or using try/catch statement to avoid the exception. If you still have doubts, could you post the related code about the API action method? It is better for us help you based on the real code. – Zhi Lv Sep 29 '20 at 12:29
1 Answers
-1
When you put your code live you want to be able to handle the exceptions.
Take a look at the Microsoft docs for exception handing here:

Callum
- 52
- 1
- 7