0

I have a function that needs to throw an exception, but I wanted it to throw that exception to the line where I called that function:

static int retrieveInt()
{
    int a = getInt();
    if(a == -1)
        throw new Exception("Number not found"); //The runtime error is pointing to this line
    return a;
}

static void Main(string[] args)
{
     int a = retrieveInt(); //The runtime error would be happening here
}
Ricardo Alves
  • 1,071
  • 18
  • 36

3 Answers3

5

After 2 hours searching I found the answer to my question. To do what I wanted it is needed to user [System.Diagnostics.DebuggerStepThrough] before the function:

[System.Diagnostics.DebuggerStepThrough]
static int retrieveInt()
{
    int a = getInt();
    if(a == -1)
        throw new Exception("Number not found"); //The runtime error will not be here
    return a;
}

static void Main(string[] args)
{
     int a = retrieveInt(); //The runtime error happens now here
}
Ricardo Alves
  • 1,071
  • 18
  • 36
  • 1
    This has no effect on where the exception is thrown. – Servy Feb 11 '15 at 16:39
  • It did work on me. Either you didn't understand my problem or you did not try what I suggested. – Ricardo Alves Feb 11 '15 at 17:08
  • It may have done what you wanted it to do, but it doesn't do what you asked in your question. – Servy Feb 11 '15 at 17:09
  • My question is pretty clear, the runtime exception is notified in the line that called the function that throws the exception. – Ricardo Alves Feb 11 '15 at 17:14
  • Your question is asking for the exception to be thrown from the caller. This answer does nothing at all to change where the exception is thrown, how it's handled, or anything at all about how the program executes. It would only ever *possibly* change where the debugger breaks when an exception is thrown, which is something not discussed at all in the question. – Servy Feb 11 '15 at 17:28
  • I most certainly read the whole question. Nowhere in the question do you so much as mention the debugger. Your question is asking something completely different than you apparently thought you were asking. – Servy Feb 11 '15 at 18:13
  • I did. It say nothing about wanting the debugger to break on one line or another, but rather says that the exception should be thrown from one line or another, which is something completely different. Again, you seem to be assuming you said a whole bunch of things that you did not in fact say. – Servy Feb 11 '15 at 18:18
  • 1
    It sounds like *you* need to go read it again. You seem to have though you've said a lot of things you haven't actually said, you should re-read your question to realize what you've actually said, versus what you thought you said. – Servy Feb 11 '15 at 18:51
  • 1
    Perfect thank you this is what I was looking for. Now my exceptionHandler throws exception in the method that called him instead of throwing it in the exceptionHandler class – Keytrap Aug 21 '21 at 18:23
2

The described behaviour is not strictly possible, but working around to the desired effect is.

The issue you're running into is that in Visual Studio, execution pauses and we see exceptions from the most available location with debug info. For framework methods, this means the method call, even though the exception is being thrown a couple of calls deeper. Since the exception is coming from the same project you're debugging, you'll always have debug info for the actual throw line, and thus you'll always reach that line.

The workaround here is to utilize the Call Stack window in VS, which will include a couple lines down the method call which triggered the error, and double-clicking on this will bring you where you want to be, including all local variables at the time of the call. This is analogous to the framework exception behaviour, because if you look at the stack trace, several frames are marked as "external" because they don't have debug info.

EDIT: To add some info about the behaviour of try and catch, catch will respond to any exception not already caught - thus, even if the exception is thrown several calls deeper, if it's not handled by the time the call stack unwinds into your try block, it'll hit the appropriate catch block (if there is one).

David
  • 10,458
  • 1
  • 28
  • 40
-1

How about this ?

public static int NewInt
{
    get
    {
            throw new Exception("Number not found");
    }
}

static void Main(string[] args)
{
    int a = NewInt;
}
Justin
  • 414
  • 4
  • 8