Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caught exception is null itself !

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?

like image 266
Xaqron Avatar asked Apr 12 '11 11:04

Xaqron


People also ask

Can a caught exception be null?

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.

Why my exception message is null?

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.

Can exception message be null Java?

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)?

Can exception message be null c#?

Yes, the constructor you are using requires a string. String. Empty is not the same as null therefore it will throw an exception.


2 Answers

For anyone ending up here, I've found an instance where this is possible (If only detectable in the debugger). VS2013 Update 4.

Broken:

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.

Fixed:

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); } 
like image 156
Ben Cull Avatar answered Oct 02 '22 02:10

Ben Cull


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.

like image 41
William Jockusch Avatar answered Oct 02 '22 00:10

William Jockusch