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
132
votes
11 answers

Java Try Catch Finally blocks without Catch

I'm reviewing some new code. The program has a try and a finally block only. Since the catch block is excluded, how does the try block work if it encounters an exception or anything throwable? Does it just go directly to the finally block?
NullPointer0x00
  • 1,808
  • 3
  • 18
  • 20
131
votes
7 answers

How do exceptions work (behind the scenes) in c++

I keep seeing people say that exceptions are slow, but I never see any proof. So, instead of asking if they are, I will ask how do exceptions work behind the scenes, so I can make decisions of when to use them and whether they are slow. From what I…
user34537
129
votes
10 answers

Multiple try codes in one block

I have a problem with my code in the try block. To make it easy this is my code: try: code a code b #if b fails, it should ignore, and go to c. code c #if c fails, go to d code d except: pass Is something like this possible?
arvidurs
  • 2,853
  • 4
  • 25
  • 36
128
votes
1 answer

powershell 2.0 try catch how to access the exception

This is the try catch in PowerShell 2.0 $urls = "http://www.google.com", "http://none.greenjump.nl", "http://www.nu.nl" $wc = New-Object System.Net.WebClient foreach($url in $urls) { try { $url …
Medo
  • 1,435
  • 3
  • 10
  • 9
124
votes
14 answers

Catching java.lang.OutOfMemoryError?

Documentation for java.lang.Error says: An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch But as java.lang.Error is a subclass of java.lang.Throwable, I can catch this type of…
Denis Bazhenov
  • 9,680
  • 8
  • 43
  • 65
121
votes
6 answers

Capture keyboardinterrupt in Python without try-except

Is there some way in Python to capture KeyboardInterrupt event without putting all the code inside a try-except statement? I want to cleanly exit without trace if user presses Ctrl+C.
Alex
  • 2,009
  • 6
  • 24
  • 27
120
votes
4 answers

Using python "with" statement with try-except block

Is this the right way to use the python "with" statement in combination with a try-except block?: try: with open("file", "r") as f: line = f.readline() except IOError: If it is, then considering the old way of doing…
new name
  • 15,861
  • 19
  • 68
  • 114
120
votes
6 answers

Is it possible in Java to catch two exceptions in the same catch block?

I need to catch two exceptions because they require the same handling logic. I would like to do something like: catch (Exception e, ExtendsRuntimeException re) { // common logic to handle both exceptions } Is it possible to avoid duplicating…
user710818
  • 23,228
  • 58
  • 149
  • 207
116
votes
16 answers

Pattern to avoid nested try catch blocks?

Consider a situation where I have three (or more) ways of performing a calculation, each of which can fail with an exception. In order to attempt each calculation until we find one that succeeds, I have been doing the following: double val; try {…
jjoelson
  • 5,771
  • 5
  • 31
  • 51
112
votes
5 answers

How to catch segmentation fault in Linux?

I need to catch segmentation fault in third party library cleanup operations. This happens sometimes just before my program exits, and I cannot fix the real reason of this. In Windows programming I could do this with __try - __catch. Is there…
Alex F
  • 42,307
  • 41
  • 144
  • 212
110
votes
11 answers

Difference between try-finally and try-catch

What's the difference between try { fooBar(); } finally { barFoo(); } and try { fooBar(); } catch(Throwable throwable) { barFoo(throwable); // Does something with throwable, logs it, or handles it. } I like the second version better…
Vijay Kotari
  • 1,945
  • 3
  • 15
  • 16
105
votes
6 answers

Node.js catch ENOMEM error thrown after spawn

My Node.js script crashes because of a thrown ENOMEM (Out of memory) errnoException when using spawn. The error: child_process.js:935 throw errnoException(process._errno, 'spawn'); ^ Error: spawn ENOMEM at errnoException…
tobi
  • 1,924
  • 2
  • 20
  • 25
104
votes
15 answers

throws Exception in finally blocks

Is there an elegant way to handle exceptions that are thrown in finally block? For example: try { // Use the resource. } catch( Exception ex ) { // Problem with the resource. } finally { try{ resource.close(); } catch( Exception…
Paul
  • 1,757
  • 2
  • 11
  • 21
103
votes
12 answers

What is the real overhead of try/catch in C#?

So, I know that try/catch does add some overhead and therefore isn't a good way of controlling process flow, but where does this overhead come from and what is its actual impact?
JC Grubbs
  • 39,191
  • 28
  • 66
  • 75
103
votes
2 answers

What are the differences between throws and rethrows in Swift?

After searching for some references to figure it out, -unfortunately- I could not find useful -and simple- description about understanding the differences between throws and rethrows. It is kind of confusing when try to understand how we should use…
Ahmad F
  • 30,560
  • 17
  • 97
  • 143