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
69
votes
7 answers

Try catch in a JUnit test

I'm writing unit tests for an application that already exists for a long time. Some of the methods I need to test are build like this: public void someMethod() throws Exception { //do something } If I want to test these methods I have to write…
Nelsch
  • 1,077
  • 2
  • 9
  • 14
68
votes
4 answers

Invalid conversion from throwing function of type (_,_,_) throws -> Void to non-throwing function type (NSData?, NSURLResponse?, NSError?) -> Void

I have written this code: func getjson() { let urlPath = "https://api.whitehouse.gov/v1/petitions.json?limit=100" let url = NSURL(string: urlPath) let session = NSURLSession.sharedSession() let task =…
Martin Mikusovic
  • 1,002
  • 2
  • 9
  • 14
67
votes
5 answers

Why is `.catch(err => console.error(err))` discouraged?

I'm using promises and have code that looks like the following: function getStuff() { return fetchStuff().then(stuff => process(stuff) ).catch(err => { console.error(err); }); } Or: async function getStuff() { try { const…
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
66
votes
9 answers

Performance of try-catch in php

What kind of performance implications are there to consider when using try-catch statements in php 5? I've read some old and seemingly conflicting information on this subject on the web before. A lot of the framework I currently have to work with…
Travis
  • 848
  • 1
  • 7
  • 10
65
votes
4 answers

If an Exception happens within a using statement does the object still get disposed?

If an Exception happens within a using statement does the object still get disposed? The reason why I'm asking is because I'm trying to decide on whether to put a try caught around the whole code block or within the inner using statement. Bearing in…
Andrew
  • 9,967
  • 10
  • 64
  • 103
65
votes
7 answers

When to use Try Catch blocks

Ok, this might be a very noob question, but I find that PHP Documentation on that and several Internet Searches hasn't give me any idea about that. When should I use try-catch blocks to improve my application? I read someone saying that we should…
Shoe
  • 74,840
  • 36
  • 166
  • 272
65
votes
8 answers

Javascript set const variable inside of a try block

Is it possible in ES6 to set a variable inside of a try{} using const in strict mode? 'use strict'; const path = require('path'); try { const configPath = path.resolve(process.cwd(), config); } catch(error) { …
Justin
  • 42,716
  • 77
  • 201
  • 296
65
votes
8 answers

Multiple return statements without compiler error

This was an interview question: public class Demo { public static void main(String[] args) { System.out.println(foo()); } static String foo() { try { return "try ..."; } catch (Exception e) { …
user2575725
64
votes
10 answers

A better way to validate URL in C# than try-catch?

I'm building an application to retrieve an image from internet. Even though it works fine, it is slow (on wrong given URL) when using try-catch statements in the application. (1) Is this the best way to verify URL and handle wrong input - or should…
Benny Skogberg
  • 10,431
  • 11
  • 53
  • 83
64
votes
5 answers

sql try/catch rollback/commit - preventing erroneous commit after rollback

I am trying to write an MS sql script that has a transaction and a try/catch block. If it catches an exception, the transaction is rolled back. If not, the transaction is committed. I have seen a few different websites saying to do it like…
user3666839
  • 671
  • 1
  • 6
  • 4
64
votes
4 answers

Java - is it bad practice to do a try/catch inside a try/catch?

I have some code that I want to execute if an exception happens. But that code can also generate an exception. But I have never seen people do a try/catch inside another try/catch. Is what I am doing poor practice and maybe there is a better way of…
GeekedOut
  • 16,905
  • 37
  • 107
  • 185
63
votes
3 answers

Exception handling try catch inside catch

I recently came across code written by a fellow programmer in which he had a try-catch statement inside a catch! Please forgive my inability to paste the actual code, but what he did was something similar to this: try { //ABC Operation } catch…
topgun_ivard
  • 8,376
  • 10
  • 38
  • 45
62
votes
7 answers

Is it a known good practice to use a big try-catch per method in java?

I've been interviewed recently and the interviewer wanted me to do a technical test to see my knowledge. After I finished it he gave me feedback about how I did it, which I didn't expect and I appreciated, since few interviewers do it if they don't…
Adrián Pérez
  • 2,186
  • 4
  • 21
  • 33
62
votes
8 answers

New/strange Java "try()" syntax?

While messing around with the custom formatting options in Eclipse, in one of the sample pieces of code, I saw code as follows: /** * 'try-with-resources' */ class Example { void foo() { try (FileReader reader1 = new…
Ali
  • 12,354
  • 9
  • 54
  • 83
61
votes
8 answers

Flutter and Dart try catch—catch does not fire

Given the shortcode example below: ... print("1 parsing stuff"); List subjectjson; try { subjectjson = json.decode(response.body); } on Exception catch (_) { print("throwing new error"); throw…
John Gorter
  • 2,162
  • 2
  • 17
  • 25