9

I want to convert a string into exception but not able to find any thing on google.

I am using C# .Net 2.0.

Reason is because third party client has a method that is logging method and only takes exception and i have a scenario where i must need to log something but using that method. so must need to convert string into exception.

4
  • This should have popped up very, very easily on Google: msdn.microsoft.com/en-us/library/system.exception.aspx Commented Aug 30, 2013 at 13:55
  • what keywords should i had used to search it ? Commented Aug 30, 2013 at 13:58
  • I searched for [exception string C#]. The Exception constructor was the second result returned. Commented Aug 30, 2013 at 14:03
  • I tried "convert string to exception c#" and nothing related came up, thanks anyway Commented Aug 30, 2013 at 14:07

2 Answers 2

26

Exceptions are created as any other object, using the new keyword. You can provide it a message argument that you can store your string in:

Exception e = new Exception("Your string goes here");
Sign up to request clarification or add additional context in comments.

Comments

0

If you are using Try...Catch() then you can do following to add your customize message and original exception together

try{
    //your code block
}
catch(Exception e)
{
    var exception = new Exception("Your message: ");
    //Display "exception" to users
    //Log "e" for further investigation 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.