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
81
votes
8 answers

How to catch the fatal error: Maximum execution time of 30 seconds exceeded in PHP

I've been playing around with a system I'm developing and managed to get it to cause this: Fatal error: Maximum execution time of 30 seconds exceeded It happened when I was doing something unrealistic, but nevertheless it could happen with a…
ingh.am
  • 25,981
  • 43
  • 130
  • 177
81
votes
1 answer

Dart catch clause

I recently stumbled across the following Dart code: void doSomething(String url, String method) { HttpRequest request = new HttpRequest(); request.open(method, url); request.onLoad.listen((event) { if(request.status < 400) { …
IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756
80
votes
5 answers

Using catch without arguments

What is the difference between: catch { MessageBox.Show("Error."); } and: catch (Exception ex) { MessageBox.Show("Error."); //we never use ex, so is it better to use catch without arguments? }
petko_stankoski
  • 10,459
  • 41
  • 127
  • 231
80
votes
8 answers

How to efficiently use try...catch blocks in PHP

I have been using try..catch blocks in my PHP code, but I'm not sure if I've been using them correctly. For example, some of my code looks like: try { $tableAresults = $dbHandler->doSomethingWithTableA(); $tableBresults =…
ILikeTacos
  • 17,464
  • 20
  • 58
  • 88
79
votes
5 answers

Throwing exceptions in a PHP Try Catch block

I have a PHP function in a Drupal 6 .module file. I am attempting to run initial variable validations prior to executing more intensive tasks (such as database queries). In C#, I used to implement IF statements at the beginning of my Try block that…
kaspnord
  • 1,403
  • 2
  • 18
  • 28
78
votes
5 answers

Catching multiple exceptions at once in Scala

How to catch multiple exceptions at once in Scala? Is there a better way than in C#: Catch multiple exceptions at once?
TN.
  • 18,874
  • 30
  • 99
  • 157
78
votes
7 answers

How to catch this error: "Notice: Undefined offset: 0"

I want to catch this error: $a[1] = 'jfksjfks'; try { $b = $a[0]; } catch (\Exception $e) { echo "jsdlkjflsjfkjl"; } Edit: in fact, I got this error on the following line: $parse = $xml->children[0]->children[0]->toArray();
meotimdihia
  • 4,191
  • 15
  • 49
  • 69
77
votes
3 answers

Catch statement does not catch thrown error

For some reason this code gives me an uncaught exception error. It seems the catch block is not catching the error. Are try catch blocks scoped in such a way that I cannot throw an error in a nested function, and then expect it to be caught by a…
Xenology
  • 2,357
  • 2
  • 21
  • 39
76
votes
6 answers

Exception traceback is hidden if not re-raised immediately

I've got a piece of code similar to this: import sys def func1(): func2() def func2(): raise Exception('test error') def main(): err = None try: func1() except: err = sys.exc_info()[1] pass # some…
parxier
  • 3,811
  • 5
  • 42
  • 54
75
votes
10 answers

General decorator to wrap try except in python?

I'd interacting with a lot of deeply nested json I didn't write, and would like to make my python script more 'forgiving' to invalid input. I find myself writing involved try-except blocks, and would rather just wrap the dubious function up. I…
Mittenchops
  • 18,633
  • 33
  • 128
  • 246
73
votes
3 answers

Ruby equivalent for Python's "try"?

I'm trying to convert some Python code into Ruby. Is there an equivalent in Ruby to the try statement in Python?
thatonegirlo
  • 893
  • 1
  • 6
  • 10
72
votes
8 answers

Catch Multiple Custom Exceptions? - C++

I'm a student in my first C++ programming class, and I'm working on a project where we have to create multiple custom exception classes, and then in one of our event handlers, use a try/catch block to handle them appropriately. My question is: How…
Alex
  • 64,178
  • 48
  • 151
  • 180
71
votes
5 answers

Python Try Catch Block inside lambda

Is it possible to use try catch block inside of a lambda function. I need the lambda function to convert a certain variable into an integer, but not all of the values will be able to be converted into integers.
scriptdiddy
  • 1,207
  • 3
  • 17
  • 31
70
votes
3 answers

Catching multiple exceptions in Java-8

While trying out the multi-catch feature I found in my m1() method everything is working fine as expected. However, in m2() the same code does not compile. I have just changed the syntax to reduce the number of lines of code. public class Main { …
T-Bag
  • 10,916
  • 3
  • 54
  • 118
70
votes
3 answers

How to get the last exception object after an error is raised at a Python prompt?

When debugging Python code at the interactive prompt (REPL), often I'll write some code which raises an exception, but I haven't wrapped it in a try/except, so once the error is raised, I've forever lost the exception object. Often the traceback and…
Ben Hoyt
  • 10,694
  • 5
  • 60
  • 84