I am trying to execute some statements within Try block. I am calling an API and performing some operations. There is one specific operation which is resulting in RuntimeError which is puked while running the program, although I am catching it with an exception. How do I go about avoiding the errors being puked from Try block?
try:
call API and perform some tasks.
encounters an error here
Except RunTimeError as ex:
print(ex)
Well as Susmit R. Veena said the point of try and except is to catch the error thrown in the try block.
In case you have some flow you want to ignore from its exceptions in the try block, than you can have a nested try catch block for the specific error you want to ignore and keep doing the logic after it is ignored.
for example:
try:
try:
call API and perform some tasks.
encounters an error here
Except TheErrorYouWantToIngore:
pass
keep on doing some stuff even TheErrorYouWantToIngore has been throwed
Except RunTimeError as ex:
print(ex)
the whole point of try and except is to catch the errors thrown by the code in try block in except block. Post the code for solution to the error
Related
Why is does my step function stop executing instead of following the Catch #1 path?
Because you cannot catch States.Runtime errors. The docs state
States.Runtime
An execution failed due to some exception that it couldn't process. Often these are caused by errors at runtime, such as attempting to apply InputPath or OutputPath on a null JSON payload. A States.Runtime error isn't retriable, and will always cause the execution to fail. A retry or catch on States.ALL won't catch States.Runtime errors.
You tried accessing FaceDetails[0]... but there aren't any FaceDetails in your state.
Instead of catching this error you should put a Choice state after the DetectFace to determine whether you actually found a face and only then forward that face detection to the lambda. You can use a IsPresent check for $.FaceDetails[0]: https://stackoverflow.com/a/65693332/2442804 .
I have an app where an AJAX error window pops up when a constraint for data. I want this AJAX error to not show up. It only does it for one of the fields in the table and not others with similar constraints. Is there a way to get rid of this window?
The error usually appears when a certain process fails due to your constraint exception.
You can either supply an own error message (this will simply replace the constraint message) or handle the exception yourself, like this:
declare
lExConstraint exception;
pragma exception_init(lExConstraint, -2290);
begin
insert into your_table(columnx) values (1);
exception
when lExConstraint then
-- do your handling here
null;
end;
In this example, I created an alias for ORA-02290, a check constraint exception - but it could be used in a similar way for all other exceptions. Note that you should not leave the code like this, as it will simply swallow the exception and do nothing.
I am trying to integrate stripe with my django project, so I try to create a PaymentIntent and if a network communication with stripe failed I will try to do like this:
try:
intent = stripe.PaymentIntent.create(
amount=100,
currency='usd'
)
return JsonResponse({
'clientSecret': intent['client_secret']
})
except stripe.error.CardError as e:
pass
except stripe.error.RateLimitError as e:
pass
except stripe.error.InvalidRequestError as e:
pass
except stripe.error.AuthenticationError as e:
pass
except stripe.error.APIConnectionError as e:
try:
intent_cancel = stripe.PaymentIntent.cancel(
intent['id']
)
except Exception as e:
# (If an exception is raised this means that the PaymentIntent was not created, I am right ?)
# I redirect the user to the payment page and inform him
# that a network problem has occurred and ask him to repeat his request
pass
except stripe.error.StripeError as e:
pass
except Exception as e:
pass
My questions are:
1 Is my way of handling the exception right ?
2 Can I apply this logic to the other exception ?
3 In the documentation they say that we should use an idempotency_key to retry the failed requests, how can I implement this ? and what about if I retry the failed request and it fails again, what should I do ?
You have 2 Stripe API requests in the same try block. This means if one succeeds but the other fails due to a connection error you'll treat both as failed. A better flow would be to only have one API operation per try/catch block.
You also have a request to cancel a hardcoded PaymentIntent after your code returns a JSON object. Since the return isn't conditional this is likely dead code.
The cancel logic in the case of a connection error doesn't make sense here. You should only hit this path if the connection failed and the intent wasn't created, so trying to cancel the non-existent PaymentIntent will likely result in yet another error. Instead you should introduce retry logic here. Luckily stripe-python has this built in specifically for network errors: https://github.com/stripe/stripe-python#configuring-automatic-retries
Once all retries have failed, you should probably log that somewhere and inform your user that a problem occurred and that they should try again later.
As for idempotency keys, the Stripe docs have a primer on that here: https://stripe.com/docs/api/idempotent_requests?lang=python
I was going to use send_mail function to send email, and I was hoping that using fail_silently=True would prevent this function from raising exceptions.
It turns out it does work if SMTPException is raised; however I noticed it doesn't intercept socket.error exception - so if STMP server is down, an exception will be raised even with fail_silently=True
I'm now wondering how to get the complete list of exceptions raised by send_mail so I can catch them in try/except loop. Any suggestions?
I'd say that catch an Exception is a very final approach. Since both SMTPException and socket.error are children of IOError you can catch an IOError instead. If you'll ever catch something else - you can add this to your catch list later.
I had been trying hard to figure out why the exceptions thrown from StartElement event handler are not being caught by my application which makes use of expat parser( in C). The application just terminates saying that it cannot find catch blocks, though I have all the catch blocks in place. It is just that since exceptions are being thrown from my own event handlers, XML_Parse API of expat is unable to pass it on to my code where I have all the catch blocks. One of the stackoverflow user with name 'Michael Anderson" suggested to rebuild expat lib with necessary gcc flags to make expat lib handle exceptions. Can someone let me know what flags are those? Or Suggest a better way out to handle errors in event handlers like startelement, endelement etc.
I somehow want XML_Parse API to return 0 if I encounter any exception in my registered event handlers. Please help. Thanks in advance.
Here is the code:
try
{
if( ! XML_Parse(.....) )
{
throw exception;
}
}
catch(...)
{
}
In the working scenario, if XML_Parse encounters a malformed xml file, it promptly returns zero, and I get into if block and throw exception, and it is caught fine.
But in the problematic case, the exceptions are being thrown from the event handlers but my application dumps core, and core stack says that it cannot find catch and finally calling std::terminate and abort.
Now, how do I make XML_Parse to return zero when I want to throw user defined exception from event handlers?
As per expat.h, you should invoke XML_StopParser(parser, 0) when you encounter an error in your handler that warrants aborting the parse.
XML_Parse will then return XML_FALSE. At that point you can invoke your application-specific error handling