Get exception number...

AlexCode

Senior Contributor
Joined
Jul 7, 2003
Messages
931
Location
Portugal
Is there a way to retrieve the number of the exception that is thrown by a Try Catch?

It give the message but sometimes it's helpfull to have the number too...


Thanks

Alex :D
 
Depends on the exception some like a SQLException contain an error number property based on the error code raised by SQL, but most do not.
Unlike VB6 where the error number was all you had to go on to determine the problem exceptions are more OO - the exception itself denotes the type of error (InvalidCastException, OverflowException, DivideByZeroException etc.)

Could you give a code sample of where you would need such a number - there may be an alternate way of solving the problem.
 
I got this need when my app thrown me an exception saing something like this: "The application could not start...".

The big problem is that I'm Portuguese and this exception was in Portuguese and so I had some dificulty finding any resourses about it on the Net.

If the exception had a number, no matter what language your OS is, that number would always remain the same...

Althow I successfully trasnslated the exception and found what it was searching on Google.


Thanks

Alex :D
 
The type of exception never changes between languages either though. The following snippet may give a better idea.

Visual Basic:
Dim i As Integer
Try
	i = Integer.Parse(TextBox1.Text)
Catch ex As FormatException
       'handle invalid data type here

Catch ex As OverflowException
	'handle nuber being too large here
Catch ex As Exception
	'anything else here
	MessageBox.Show(ex.ToString)
End Try

and you could always get the underlying exception type as a string for logging with something similar to
Visual Basic:
MessageBox.Show(GetType(Exception).ToString())
 
I know that the type of the exceptions doesn't change, but the message displayed to the user its OS dependant and so, if the SO its in Portuguese the message its in the same language.

I belive this overlaped their minds... They only wanted to hide the supposable unecessary info but the error number is quite the only way anyone can search it around the net...

When you gave me that code with all those Try/Catch, those exceptions don't have all possible exception types... specially if the exception was thrown by th OS itself...

Either way... I got the point and already solved my problem translating the exception...


Thanks !

Alex :D
 
Surely you can search on the exception name though - rather than the error message itself.
Agreed my sample doesn't have handlers for all exceptions - I just used the ones that made most sense for the likey errors to occur in that given situation, as for exceptions raised by the OS itself not too sure what you mean there could you give an example?
 
Last edited:
This "need" apear to me about a week ago with an error just like this: "A aplicacao nao pode arrancar."... translating it's "The application couldn't start."... after this just a bunch of "0xffff0fefffx0000"... You know...

I can't really remember but was something related with my database. Cute isn't it? :p

Alex :D
 
Was that a .Net error or one thrown by windows itself? If it is an OS error then it is probably happening before your application runs in this case so .Net exceptions wouldn't be able to handle it anyway.
 
Back
Top