Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
1.44/5 (3 votes)
See more:
Given the fillowing set of functions , will the exception thrown by func10 be caught by the try clause in func1?
If no, why.If yes why.

int func10()
{
    throw 25.00;
}

int func9()
{
      func10();
}

int func8()
{
    func9();
}

int func7()
{
   func8();
}


int func6()
{
   func7();
}

int func5()
{
     func6();
}

int func4()
{
   func5();
}

int func3()
{
  func4();
}

int func2()
{
   func3()
}

int func1()
{
      func2();
}

int main()
{
     try
       {
             func1();
        }
        catch(double dValue)
        {
            return 1;
         }
         return 0;
}
Posted
Updated 25-Apr-14 8:12am
v3
Comments
Sergey Alexandrovich Kryukov 24-Apr-14 17:53pm    
The question is absurd: you have only one try-catch block. Look, such questions are not helpful. You need to learn how exceptions work from scratch.
You should not catch too locally, you should let them propagate.
—SA
Gbenbam 25-Apr-14 12:18pm    
I dont think d qestion is absurd.It was carefully designed to help clear a personal confusion.How many try/catch block do u recommend?What do you mean by letting them propagate?Can u kindly expanciate?

Please see my comment to the question: the problem is only one: you have no clue. To understand how exception works, you need to understand how thread works and, desirably, a thread. Hope my past answers can give you the idea:
Does Exception in C# Constructor Cause Caller Assignment to Fail?[^],
where was stored .net exceptions in operating system[^].

Exception handling is done for isolating exceptional behavior from "regular", therefore, don't catch locally, let exceptions propagate.

[EDIT]
Gbenbam asked:
I have read your past answers but for me to fully comprehend them I will appreciate it if you answer the following questions:How many try/catch block do you recommend for this my hypothetical example? What do you mean by letting them propagate (can you kindly expanciate [? — SA])?
This can only be explained in a complex. Please see more detailed set of links to my past answers:

Where you catch all exceptions:
When i run an application an exception is caught how to handle this?[^].

Re-throwing:
throw . .then ... rethrowing[^].

Exception should be thrown in very few strategically selected places; I call then "competence point", where you know exactly how to interpret them. Besides, on top of stack of each thread (not always, for UI, there are special point predefined in the libraries, in the main event cycle of the UI application, please see below).

In UI:
For forms:
Error Logging and Screen Shot.[^],
Catching an Exception[^].

On exceptions on top of stack frame, inner exceptions, re-throwing, using StackTrace: Exception Details: System.Runtime.InteropServices.COMException: Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error:...[^].

Many aspects together: Handling exceptions in class library (dll)[^].

—SA
 
Share this answer
 
v2
Comments
Gbenbam 25-Apr-14 11:43am    
I have read your past answers but for me to fully comprehend them I will appreciate it if you answer the following questions:How many try/catch block do u recommend for this my hypothetical example?What do you mean by letting them propagate(Can u kindly expanciate)?
Sergey Alexandrovich Kryukov 25-Apr-14 14:47pm    
Very, very few catches, in many cases, only one per thread, sometimes more. This is not defined by number, is a matter of particular application-dependent strategy.
I will give you more links, please wait a bit.
—SA
Sergey Alexandrovich Kryukov 25-Apr-14 14:57pm    
Please see my update to the answer, after [EDIT]. Please, pay a lot of attention. The exception mechanism runs over the usual call/return mechanism, this is a time machine, "long-jump" mechanism over the stack frames. Very non-trivial. It's more non-trivial than you probably think and much easier to use than you probably think.

And consider accepting my answer formally (green "Accept" button)... :-)

—SA
Gbenbam 25-Apr-14 14:25pm    
I have made important modification to my question,please, what do you mean by letting the exceptions propagate.Do you mean catching and rethrowing them?Perhaps you see the modification I made to my question?The question is designed such that it will help clear confusions that recently arose in regards to my understanding of exceptions
Sergey Alexandrovich Kryukov 25-Apr-14 14:46pm    
No, I mean not handling them at all. Exception do propagate up the stack. All exceptions should be handled far on top, in few points. Rethrowing is only needed when you do part of handling here, and other part of handling up the stack (this "up the stack" should be the key of understanding). Often, you need to throw exception of some other type. You interpret original exception, create exception of more or less abstract type and throw it; this is more advanced strategy.
—SA
No, the catch would only capture any exceptions thrown within the scope of the try.
 
Share this answer
 
Comments
Matt T Heffron 24-Apr-14 13:32pm    
Literally, that is true, but incomplete.
When func1() is called, it *will* catch the exception thrown in func10()
Any other path into func10() will not be caught because the try in func1() doesn't encompass that execution path.
Richard C Bishop 24-Apr-14 14:58pm    
I see what you are saying, good point.
Gbenbam 25-Apr-14 14:35pm    
Considering your answer , Which is better?To catch the exception as done in the example above or to catch and rethrow after each function until it is finally caught by the last catch clause shown in the example above.By the way, whzt do you think S.A meant by letting the exception propahate?I have asked him but hr has not replied me yet?
Matt T Heffron 25-Apr-14 16:56pm    
Sergey has edited his Solution above to address this question.
All I would add is that the example you've shown is clearly very contrived, presumably to demonstrate that exceptions "operate" across the call stack, not just within methods.
Disclaimer: I haven't read all of the articles Sergey referenced so some of this may be redundant with those.
Exceptions should be used for exceptional circumstances, not for simple error handling or failure indication for which other techniques are better suited. For example, returning an out-of-range value for failure like ftell(FILE*) returns -1L on error. Or strstr returns NULL if the string being searched for is not found. Maybe returning a NaN on error from a function with return type of double.
As to WHERE to catch the exception, Sergey's "competence point" is correct. That should be as close to the point of the exception being thrown as possible and still meet the conditions of "competence point". I.e., the point where you can handle the condition "appropriately" and continue execution of the application "cleanly". Do NOT just catch and re-throw at every function!
Gbenbam 25-Apr-14 19:13pm    
Thanks a lot.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900