Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

When I catch an error using "try - catch" keywords, how can I get the error number / code?

 

I don't see nothing by using: "catch(Exception ex)". "ex." does not lists anything regarding this :(

 

Thank you

Posted
ex should have all the info you need.

See ex.Message and ex.HResult

 

The ex.Message returns the errors text message (like it's own name says) not the errors code itself, yet, "ex." does not contains HResult...

Posted
HResult is a protected member of Exception. If you want access to it then you need to derive a class from Exception. Out of curiosity why would you need to access an error code?
"Who is John Galt?"
Posted

I need it to execute spesific code depending the kind of error...

 

I Need to recreate my own message errors that is.

 

PS:What do you mean by this: "...derive a class from Exception"?

Posted
Couldn't you just catch different types of exceptions?

 

Derive from exception means:

 

public class MyException : Exception

 

 

I have to create a class? am not quite understanding what is what are you trying to tell me... could you be more specific? I mean, this is the 6th post in this threat and I havn't learn to retrieve the exception errors....

  • Administrators
Posted (edited)

Rather than catching Exception you should be catching one of it's derived classes. If you suspect code may overflow catch OverflowException, if you are dealing with SQL catch SQLException etc.

If you want a more specific example post a little code snippet indicating what is failing and I'll knock up a sample.

Edited by PlausiblyDamp

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

In VB6 I could get "error.Number" to retrieve error code (430, 560, whatever)

 

I just want to do the same in C#!!! (if it is possible?!)

 

No matter if it will be triggered by a wrong SQL syntax, pointing to an non-existing item index in a ComboBox, trying to convert a non-numeric value to a numeric value within a string var, or if my gramy spills food all over the floor.

 

I just need to do this:

 

try
 {
      //This trigger the error
      EvacuateDog(speed 100, "Korn can");
 }
  catch
 {
      //Gets the ******* error number
      ErrorNum = Get.Error.Number();

      if (ErrorNum == 666)
      {
            MessageBox.Show("No can do! The dog does not fit the korns can because the can's already contains an elephant.")
      }
 }

End of story!

 

If no one understands this now and keep asking me questions I will just assume that I have seroius metal problem, my english is realy bad and I will just close this thread.

 

Cheers!

Posted

In C# you can determine the error by the type of exception. There are quite a few exception types that you can catch. What you do is:

 

try 
{
      //This trigger the error
      EvacuateDog(speed 100, "Korn can");
}
catch(DogToSlowException e)
{
 MessageBox.Show("Sadly the dog was to slow and didnt make it in time, 1 dog less");
 BuryDog();
}
catch(ThereIsNoDogException e)
{
MessageBox.Show("There is no dog to evacuate, no need to evacuate");
}
catch(Exception e)
{
//This will catch all errors not handled by the previous 2 catch statements.
//After an error is cought, it can't be handled at the same level anymore.
// You can still throw it again so the error is propagated further back in the call stack
MessageBox.Show("Something went wrong");
}
catch(OverflowException e)
{
//this error handler will never be used. All exceptions are already handled
//before this catch statement by the generic catch (Exception e) statement.
}

DogToSlowException and ThereIsNoDogException would inherit from the Exception class and thrown by the EvacuateDog method.

 

To determine how to handle the error, you dont look at the err.number, but at the type of error object that is thrown. You can use several catch blocks for each try block. You can still create your own exception and give it additional information that you can use for better error handling your specific errors.

Nothing is as illusive as 'the last bug'.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...