in Education by
Here's a question to expose my lack of experience: I have a method DoSomething() which throws an exception if it doesn't manage to do it cleanly. If it fails, I try the less accurate method DoSomethingApproximately() several times in the hope that it will find a sufficiently good solution; if this also fails I finally call DoSomethingInaccurateButGuaranteedToWork(). All three are methods belonging to this object. Two questions: first, is this (admittedly ugly) pattern acceptable, or is there a more elegant way? Second, what is the best way to keep track of how many times I have called DoSomethingApproximately(), given that it is likely to throw an exception? I am currently keeping a variable iNoOfAttempts in the object, and nesting try blocks... this is horrible and I am ashamed. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You should never use exceptions for control flow of your application. In your case I'd bunch the three methods together into a single method and have it return which particular approach succeeded, maybe with an enum or something like that.

Related questions

0 votes
    Which of these exception is thrown by close() and read() methods? (a) IOException (b) FileException (c) ... & Applets of Java Select the correct answer from above options...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
0 votes
    Which of these exception is thrown in cases when the file specified for writing is not found? (a) IOException (b ... & Applets of Java Select the correct answer from above options...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    Which exception is thrown when java is out of memory? (a) MemoryFullException (b) MemoryOutOfBoundsException (c ... and answers pdf, java interview questions for beginners...
asked Oct 26, 2021 in Education by JackTerrance
0 votes
    What will be the step of the interpreter in a jump statement when an exception is thrown? (1)The ... nearest enclosing exception handler (4)The interpreter throws an error...
asked May 18, 2021 in Technology by JackTerrance
0 votes
    I have a VB6 program that someone recently helped me convert to VB.NET In the program, when saving ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 27, 2022 in Education by JackTerrance
0 votes
    I have a VB6 program that someone recently helped me convert to VB.NET In the program, when saving ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 27, 2022 in Education by JackTerrance
0 votes
    Which pattern collects and reports all exceptions to a centralized exception tracking service? A. Distributed tracing B. Audit logging C. Application logging D. Exception tracing...
asked Jan 11, 2023 in Education by JackTerrance
0 votes
    Which pattern collects & reports all exceptions to a centralized exception tracking service (a)Audit logging (b)Application logging (c)Distributed tracing (d)Exception tracing...
asked Oct 21, 2020 in Technology by Editorial Staff
0 votes
    I have added external dll files in the script task. The code is working when I am debugging the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 26, 2022 in Education by JackTerrance
0 votes
    I have added external dll files in the script task. The code is working when I am debugging the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    Which of these keywords must be used to handle the exception thrown by try block in some rational manner? (a ... Handling of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Which of the following operators is used to generate instance of an exception which can be thrown using throw? (a ... Handling of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
...