Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

164
Vistas
.Net how to detect if built-in exception is thrown manually via code in executing assembly

In my code there are lot of areas where we manually "throw" InvalidOperationException. We have specifc handlers for handling this exception type. However there is a requirement that we have to write some logic based on the fact if the exception came beacuse of a throw or it came from the system. Below is the example:

 static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Hello World!");
                //throwFirstLayer(); //if i uncomment this i am throwing the error manually
                var people = new List<Person>();

                people.Add(new Person("John", "Doe"));
                people.Add(new Person("Jane", "Doe"));
                people.Sort(); //Suppose in this line system throws InvalidOp exception
                foreach (var person in people)
                    Console.WriteLine("{0} {1}", person.FirstName, person.LastName);
            }
            
            catch(InvalidOperationException ioex)
            {
                var e = ioex;
            }
}

public static void throwFirstLayer()
        {
            throw new InvalidOperationException("manual");
        }

 public class Person
    {
        public Person(String fName, String lName)
        {
            FirstName = fName;
            LastName = lName;
        }

        public String FirstName { get; set; }
        public String LastName { get; set; }
    }

here inside the catch block i want to detect if the exception had been thrown by me manually from the code or the system raised it due to an actual invalid operation?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

you should first create your own exception class:

   public class MyInvalidOperationException : Exception
   {

   }

and throw your custom exception when you need to. (manual ones)

throw new MyInvalidOperationException();
over 4 years ago · Santiago Trujillo Denunciar

0

You could examine the stack trace of the exception and see where it originated from - but that's slow, and error-prone in production due to JIT optimizations.

I would question the wisdom of a design that needs this in the first place, but if you really, really, really need to do so, I'd suggest creating your own specific class derived from InvalidOperationException - make sure you only ever throw that (rather than plain InvalidOperationException), and then you can check whether any given instance has a concrete type that is your custom exception type or not.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda