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
211
votes
21 answers

Should try...catch go inside or outside a loop?

I have a loop that looks something like this: for (int i = 0; i < max; i++) { String myString = ...; float myNum = Float.parseFloat(myString); myFloats[i] = myNum; } This is the main content of a method whose sole purpose is to return…
Michael Myers
  • 188,989
  • 46
  • 291
  • 292
210
votes
15 answers

How using try catch for exception handling is best practice

while maintaining my colleague's code from even someone who claims to be a senior developer, I often see the following code: try { //do something } catch { //Do nothing } or sometimes they write logging information to log files like following…
Toan Nguyen
  • 11,263
  • 5
  • 43
  • 59
205
votes
20 answers

Why is try {...} finally {...} good; try {...} catch{} bad?

I have seen people say that it is bad form to use catch with no arguments, especially if that catch doesn't do anything: StreamReader reader=new StreamReader("myfile.txt"); try { int i = 5 / 0; } catch // No args, so it will catch any…
mbeckish
  • 10,485
  • 5
  • 30
  • 55
187
votes
9 answers

How do I prevent node.js from crashing? try-catch doesn't work

From my experience, a php server would throw an exception to the log or to the server end, but node.js just simply crashes. Surrounding my code with a try-catch doesn't work either since everything is done asynchronously. I would like to know what…
TiansHUo
  • 8,509
  • 7
  • 45
  • 57
186
votes
6 answers

How to capture no file for fs.readFileSync()?

Within node.js readFile() shows how to capture an error, however there is no comment for the readFileSync() function regarding error handling. As such, if I try to use readFileSync() when there is no file, I get the error Error: ENOENT, no such file…
Metalskin
  • 3,998
  • 5
  • 37
  • 61
181
votes
13 answers

Is try {} without catch {} possible in JavaScript?

I have a number of functions which either return something or throw an error. In a main function, I call each of these, and would like to return the value returned by each function, or go on to the second function if the first functions throws an…
pimvdb
  • 151,816
  • 78
  • 307
  • 352
170
votes
6 answers

Java exception not caught?

I have a small theoretical problem with try-catch constructions. I took a practical exam yesterday about Java and I don't understand following example: try { try { System.out.print("A"); throw new Exception("1"); } catch…
Kousalik
  • 3,111
  • 3
  • 24
  • 46
166
votes
8 answers

How to catch integer(0)?

Let's say we have a statement that produces integer(0), e.g. a <- which(1:3 == 5) What is the safest way of catching this?
Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
155
votes
9 answers

Why does a return in `finally` override `try`?

How does a return statement inside a try/catch block work? function example() { try { return true; } finally { return false; } } I'm expecting the output of this function to be true, but instead it is false!
bonfo
  • 2,110
  • 3
  • 17
  • 14
152
votes
8 answers

Better to 'try' something and catch the exception or test if it's possible first to avoid an exception?

Should I test if something is valid or just try to do it and catch the exception? Is there any solid documentation saying that one way is preferred? Is one way more pythonic? For example, should I: if len(my_list) >= 4: x = my_list[3] else: …
chown
  • 51,908
  • 16
  • 134
  • 170
147
votes
6 answers

How to do try catch and finally statements in TypeScript?

I have error in my project, and I need to handle this by using try, catch and finally. I can use this in JavaScript but not in Typescript. When I put Exception as argument in typescript catch statement, why it is not accepting this? here is the…
Shanmugaraja_K
  • 1,914
  • 3
  • 13
  • 25
143
votes
6 answers

Is it bad practice to return from within a try catch finally block?

So I came across some code this morning that looked like this: try { x = SomeThingDangerous(); return x; } catch (Exception ex) { throw new DangerousException(ex); } finally { CleanUpDangerousStuff(); } Now this code compiles fine…
lomaxx
  • 113,627
  • 57
  • 144
  • 179
138
votes
7 answers

How can I catch all exceptions thrown through reading / writing a file?

In Java, is there any way to get (catch) all exceptions instead of catching the exceptions individually?
Johanna
  • 27,036
  • 42
  • 89
  • 117
135
votes
10 answers

C# catch a stack overflow exception

I have a recursive call to a method that throws a stack overflow exception. The first call is surrounded by a try catch block but the exception is not caught. Does the stack overflow exception behave in a special way? Can I catch/handle the…
Toto
  • 7,491
  • 18
  • 50
  • 72
133
votes
6 answers

Correct Try...Catch Syntax Using Async/Await

I like the flatness of the new Async/Await feature available in Typescript, etc. However, I'm not sure I like the fact that I have to declare the variable I'm awaiting on the outside of a try...catch block in order to use it later. Like so: let…
freedomflyer
  • 2,431
  • 3
  • 26
  • 38