I was wanting to know if it would be possible to check for an error in a certain line of code before you run it, so that, for example, the program could notify the user that it’s about to crash before it actually crashes. This would also be useful for programs where user input needs to be formatted correctly for the thing to work.
I got this idea when I was trying to create a program that solves systems of linear equations, but my program can’t handle when a system is put into it that has infinite solutions, and the only time there is an error on a certain line is when the answer is infinite solutions. I was thinking I could skip the calculations after the error and just return the answer as “infinite solutions”
Usually if you can determine that the input of some function f will cause an error, then you check the input conditions before executing the function.
Well if you can’t predict the conditions that the error might occur and handle it, (or the conditions are too many to handle) have you tried try...catch...
?
Exception handling allows you to catch errors as they happen and handle them “gracefully”.
CodeAfter()
will not execute if LineThatErrors()
throws an error. Instead execution will jump to the catch
Just remember that throwing exceptions is expensive, as the stack has to be unwound. If you expect a lot of exceptions to be thrown, like inside a loop, then it’s better to handle the condition rather than the exception.
Why does this post have to be so complicated? Try catch and temporary variables is the way to go. Every Database does it this way.
Not sure exactly what you mean here. Can use an IF statement to check if it will explode then gracefully handle the issue which is a normal thing to do. Or a try catch block to trap the error and return something nice to the user like your string message.
Yes and no. You can do validation on the input, but that requires you to know everything that can go wrong. That’s possible in the vast majority of cases. For everything else, there are try/catch
blocks that can react to the error in a non-fatal way.
my program can’t handle when a system is put into it that has infinite solutions
Is there a loop where you can put a counter, where it signals an infinite loop if the counter exceeds some threshold?
I have code that crawls a URL to find all of its links, all of their links, etc.
Most of the time it just works — but sometimes it gets into infinite loops. I have a counter to catch that condition. Increment it every loop, and break out when it gets too big.
Try catch block? For handling error in input
Not sure about ahead of time. Maybe like an if statement when the code goes through a condition that is not supposed to, but then again try catch seems like the job
If the program gives an error and crashes when the solution is infinite, use try-catch If the program like goes through an infinite loops or something you can also limit it with like an if statement
Your example is a bit of because the textbook example explicitly states how to catch this case early on. LU decomposition, determinants and all that.
Oh, you need to catch the error.
Look in to C# error handling.
Well, yes. That’s called “defensive programming” and it’s how all software should be written.
Any monkey can write a computer program. The syntax is pretty easy to learn and it’s easy to get a program to just “work”. The hard part of software development is thinking about what can go wrong and handling it.
And most of the stuff that can go wrong is caused by users 🙂