1

I want to store an error in an array, but when I try this it tells me

System.IO.DirectoryNotFoundException is a type, which is not valid in the given context.

Type[] errorArray = new Type[] { System.IO.DirectoryNotFoundException };

Am I doing something wrong, or is it not possible this way?

1
  • 2
    Type[] errorArray = new Type[] { typeof (System.IO.DirectoryNotFoundException) } Commented Jan 2, 2021 at 15:28

2 Answers 2

4

What you want to do is use typeof().

Type[] errorArray = new Type[] { typeof(System.IO.DirectoryNotFoundException) };

This way you store the type of your exception.

Sign up to request clarification or add additional context in comments.

Comments

3

It seems you want to store the types of the exceptions in an array, not the exception itself, so you need to use typeof:

Type[] errorArray = new Type[] { typeof (System.IO.DirectoryNotFoundException) }

See this site

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.