How to catch an error and stop my bot from crashing - discord.js

So basically I'm looking for a way to stop my bot from crashing because of user error or a bug in my code. What I want to bot to do is catch an error and respond to the message sender saying "Error! ${errormessage}". hopefully you understand what I mean.

You could use try-catch or simply .catch
This is a basic javascript topic, you can read and learn error handling here
Point to note is, you need to locate the line which is throwing the error by analyzing the error log.
Keep in mind you can only prevent the bot from crashing by using the above stated methods on runtime errors otherwise known as exceptions (ex: unhandledPromiseRejection, thrown during runtime), you cannot use this method on errors like TypeError, SyntaxError etc (Errors thrown while parsing the code), such errors can only be fixed by manually identifying the line and applying appropriate fix

Related

How to use window.onerror with React?

The idea is to catch an error and then handle the information caught by the handler. But it propagate it anyway and I don't know how to stop it.
Say, I have a component and I just want to have more information about error. For some reason, if I use try-catch, the error object doesn't show the line number - unlike the window.onerror. But when I use onerror handler, it bubbles the exception and it's caught higher what is not the desired behaviour.
Do you mean the error-boundaries of React?
Have you tried this?

Uipath try/catch problem - throwing system exception

Having some trouble with try/catch in UiPath:
Got two different projects with their own workflows, with try catch implementations exactly the same in both.
However, one of the try/catch is working absolutely fine, whereas the other one is giving troubles with the following error when I Throw an exception, and then the steps defined in the catch block doesn't even trap it and execute.
Thoughts/Suggestions will be much appreciated - Thanks!
RemoteException wrapping System.Exception: <My user defined message>
at System.Activities.Statements.Throw.Execute(CodeActivityContext context)
at System.Activities.CodeActivity.InternalExecute(ActivityInstance instance, ActivityExecutor executor, BookmarkManager bookmarkManager)
at System.Activities.ActivityInstance.Execute(ActivityExecutor executor, BookmarkManager bookmarkManager)
at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)
If you have been using the Try Catch with the general Exception, it should be working in the same way in any project.
I believe you was a bit confused by the running mode.
So I assume in one project you fired the project in the debug mode. This stops on the exception when it occurs. If happening you need to hit the continue button.
But if you run the project with the Run button, it will ignore the exception handling as breakpoint and will continue now without any break.
So make sure in a live test you always run it with the run button and not with the usual debug button.

Why to use multiple catch blocks?

We can use multiple catch block in Try-Catch.
But my Question is : why to use multiple catch blocks when it can be done by using single catch block?
Suppose I want exact cause of my problem, I can get that by Ex.message
If I want to show customized message to user, I can show it by putting If-Else loop on Ex.Message.
Thanks in advance.
To handle the individual exception accordingly.
For example:
If your program is handling both database and files. If an SQLException occurs, you have to handle it database manner like closing the dbConnection/reader etc., whereas if a File handling exception then you may handle it differently like file closing, fileNotFound etc.
That is the main reason in my point of view.
For point numbers 1 and 2:
If showing error message is you main idea then you can use if..else. In case if you want to handle the exception then check the above point of my answer. The reason why I stretch the word handling is because it is entirely different from showing a simple error message.
To add some quotes I prefer Best Practices for Handling Exceptions which says
A well-designed set of error handling code blocks can make a program
more robust and less prone to crashing because the application handles
such errors.
This works only if all exceptions share the same base class, then you could do it this way.
But if you do need exception type specific handling, then I would prefer multiple try-catch blocks instead of one with type-depending if-else ...
You can also ask why do we need the Switch - Case. You can do it with If - Else.
And why do you need Else at all. You can do it with If (If not the first condition, and...).
It's a matter of writing a clean and readable code.
By using single catch clock you can catch Exception type - this practice is strongly discouraged by Microsoft programming guidelines. FxCop has the rule DoNotCatchGeneralExceptionTypes which is treated as CriticalError:
Catching general exception types can hide run-time problems from the library user, and can complicate debugging.
http://code.praqma.net/docs/fxcop/Rules/Design/DoNotCatchGeneralExceptionTypes.html
The program should catch only expected exception types (one ore more), leaving unexpected types unhandled. To do this, we need possibility to have multiple catch blocks. See also:
Why does FxCop warn against catch(Exception)?
http://blogs.msdn.com/b/codeanalysis/archive/2006/06/14/631923.aspx

Returning the error details to the calling function after catching an exception in Postgresql

I'm adding exception handling to PostgreSQL stored procedures in order to automatically rollback the transactions after an error occurs.
My problem is that once I catch the exception, then I cannot return the details of the error to the calling C program which uses libpq.
The Severity, SQLSTATE, Primary, Detail and Hint are all null. Is there a way to return these after catching the exception?
The libpq function I use to collect these values is PQresultErrorField().
Given than an exception will automatically make a postgresql transaction roll back, why catch it at all? Catching exceptions is usually only useful if you want to usefully recover from the error, not propagate it.
I have recently posted a complete solution how to add a CONTEXT to error messages on dba.SE. The trick is to call a function that raises the error / warning / notice/ etc.
I realize now that your case may be different. My workaround is for adding a CONTEXT to exceptions that you raise yourself.
If you catch an exception to do stuff before the transaction is rolled back, you may want to add a RAISE without parameters at the end of your exception block:
RAISE;
The manual about RAISE:
The last variant of RAISE has no parameters at all. This form can only
be used inside a BEGIN block's EXCEPTION clause; it causes the error
currently being handled to be re-thrown.
However, as #araqnid pointed out, there is not use in an exception block if you are going to propagate the error anyway and everything is rolled back. This solution is only useful for the rare cases where certain changes are persistent and cannot be rolled back, like dblink calls ...

Hierarchy Constraint Violation -- salesforce.com

Salesforce has thrown a new error message at me, and so far I haven't found anything useful in the docs about this. I am trying to save an object in a controller extension, and it does in fact save the record, but instead of returning to the page that I indicate, it shows me the error message "Hierarchy Constraint Violation". In looking over the debug logs, this does not show up anywhere, despite the fact that I log as error any DML exceptions and re-throw them. I am not stifling any other exceptions, either.
I can't tell what this error even means, let alone where it is coming from.
The problem turns out to be an attempt to create a self reference.

Resources