I have an ASP.NET applications. Everything was fine, but recently I get exceptions that are null themselves:
try { // do something } catch (Exception ex) { Logger.Log("Error while tried to do something. Error: " + ex.Message); }
Sometimes ex
is null
itself !
Any idea?
if in 'catch' line then hover the mouse pointer you might actually get null. You need to run the codes inside the catch statement to reference the exception to ex.
NullPointerException is a runtime exception in Java that occurs when a variable is accessed which is not pointing to any object and refers to nothing or null. Since the NullPointerException is a runtime exception, it doesn't need to be caught and handled explicitly in application code.
Thanks! So it is possible for the exception message to be null even the exception was thrown by Java/ Android itself (NOT via throw new Exception)?
Yes, the constructor you are using requires a string. String. Empty is not the same as null therefore it will throw an exception.
For anyone ending up here, I've found an instance where this is possible (If only detectable in the debugger). VS2013 Update 4.
try { // do something } catch (WebException ex) // <- both variables are named 'ex' { Logger.Log("Error while tried to do something. Error: " + ex.Message); } catch (Exception ex) // <- this 'ex' is null { Logger.Log("Error while tried to do something. Error: " + ex.Message); }
The solution is to name your exception variables differently.
try { // do something } catch (WebException webEx) // <- all good in the hood { Logger.Log("Error while tried to do something. Error: " + webEx.Message); // <- } catch (Exception ex) // <- this 'ex' correctly contains the exception { Logger.Log("Error while tried to do something. Error: " + ex.Message); }
In my case, the cause was a StackOverflowException
. Such exceptions normally don't reach the catch
block at all, but this time, for some reason I don't understand, it did reach the catch
block, but the exception was null
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With