Questions tagged [try-catch]

try-catch is a syntactic construct for catching exceptions raised by a code section

The try-catch construct for exception handling is used by a wide group of languages, like C#, C++, Matlab, Python, Java, and JavaScript. An example of this construct is shown below.

try
{
    Some code
}
catch(exception e)
{
    Handle exception
}

References:

8733 questions
3
votes
1 answer

React - Can't use Try Catch block within .map (returning the jsx from within the try catch)

Code is the following: return ( {singleProd.map(function(product) { // console.log(product) let fileID; try { …
Matthew Foyle
  • 53
  • 1
  • 6
3
votes
1 answer

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 22): ReferenceError: client is not defined

This error seems to be coming up on every http request I make. I'm not totally sure where it's coming from? (node:39390) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 22): ReferenceError: client is not defined There is…
Luke Schlangen
  • 3,722
  • 4
  • 34
  • 69
3
votes
3 answers

How to ensure that catch return value differs from try return value when returning integers or doubles

Hi everyone this is my first question so I apologies if this has been answered somewhere before but I couldn't find an answer to the question so I'm posting it here. In the case where your method has a return value that can be positive or negative,…
3
votes
1 answer

How to log stackTrace without throwing the Exception in grails

In certain situations, I'd like to catch an Exception but still show the Stack Trace in the stackTrace log defined via the Log4J properties in grails. How can I do that? I know I could write it into the "normal" log using log.error…
Jörg Brenninkmeyer
  • 3,304
  • 2
  • 35
  • 50
3
votes
3 answers

NSDictionaries and NSArrays from JSON from complicated YouTube API

I'm making an awesome iPhone app which searches for YouTube videos using the JSON API. However, Google is lazy so they just transformed the ATOM feed into JSON. Things look like this: feed->entry[0]->author[0]->name->$t This means that getting the…
user142019
3
votes
2 answers

I can't use variable assigned in Try{} scope, in main scope, but after adding return in catch{} I can?

I am trying to figure out why this code is able to use variable from try scope. If i didn't implement return to catch{} it would cause error, but with return in catch it all runs without problem, I really don't get why, I would expect that both will…
Jakub Gause
  • 309
  • 3
  • 10
3
votes
2 answers

try catch numeric inputs

I'm having a bit of trouble trying to figure out how to prevent user input that are numerical. I understand how to prevent non-numerical inputs (ie. inputting a letter instead of a number) but not the other way around. How do I work around…
Ken
  • 135
  • 10
3
votes
2 answers

Difference in placement of "try" keyword

I have a global variable of audio player. What's the difference between placing try word before the variable initialization do{ try audioPlayer = AVAudioPlayer(contentsOf: audioURL) }catch {} and placing try before calling the constructor do{ …
GeRyCh
  • 1,580
  • 3
  • 13
  • 23
3
votes
3 answers

jQuery: try catch { call function } does not work?

I'm having a lot of JavaScript on my page and I'm using typekit. In order to make my page work correctly (grid and stuff) I'm using the new typekit font events. It's simply a try and catch statement that checks if fonts get loaded or not. However…
matt
  • 42,713
  • 103
  • 264
  • 397
3
votes
3 answers

Can I have multiple try blocks with a single catch block

I'm making a chess game and I was just wondering if I can get rid of these empty catch blocks in my code and have all the try blocks within the same scope go to a single catch block because I think that this is starting to look a little stupid and…
Doomed Space
  • 31
  • 1
  • 1
  • 8
3
votes
1 answer

Lua: use of pcall

I have a piece of Lua code that is sending a udp message out, that is working. However I want to make it more error proof. So I have entered a wrong dns name to provoke a resolution failure. I do not really care what in that block fails, I just want…
Jens
  • 1,157
  • 1
  • 8
  • 17
3
votes
1 answer

logging exception and throwing new exception - is it antipattern?

If we catch exception and then we throw exception, but not the same type (just based on first one) is it still antipattern to log first one? Simple example: } catch (CloneNotSupportedException e) { log.warn(e, e.getMessage()); throw…
uncleMatuesz
  • 87
  • 1
  • 1
  • 10
3
votes
3 answers

How do I test a try-catch?

I was trying out some code in my catch method, and I wanted to try out the exception that it generates. but in order to reach the catch method, I need to crash my program, so it'll be caught and create an exception. try { //do something } catch…
Steven
  • 1,996
  • 3
  • 22
  • 33
3
votes
1 answer

.Net: Try...Catch paranoia - where does it end?

I think I have something like "programmer's OCD". I like my code to be aesthetical and clean, and I want it to be "perfect" (as in handling all possible situations correctly and prettily). Often I find myself spending a lot of time just going over…
d7samurai
  • 3,086
  • 2
  • 30
  • 43
3
votes
2 answers

How to get the error message from a C++ catch(...) block?

So, I'm looking at the C++ reference for the try/catch block. I see there are a few ways to capture an exception like so: try { f(); } catch (const std::overflow_error& e) { // this executes if f() throws std::overflow_error (same type…
CodeLikeBeaker
  • 20,682
  • 14
  • 79
  • 108