Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

150
Views
.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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!