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
103
votes
3 answers

How to catch SqlException caused by deadlock?

From a .NET 3.5 / C# app, I would like to catch SqlException but only if it is caused by deadlocks on a SQL Server 2008 instance. Typical error message is Transaction (Process ID 58) was deadlocked on lock resources with another process and has…
Joannes Vermorel
  • 8,976
  • 12
  • 64
  • 104
99
votes
1 answer

What is Round brackets / parentheses () in try catch in Java

As per as my knowledge we use try catch as follows: try { //Some code that may generate exception } catch(Exception ex) { } //handle exception finally { //close any open resources etc. } But in a code I found following try( …
Partha Sarathi Ghosh
  • 10,936
  • 20
  • 59
  • 84
99
votes
6 answers

Writing try catch finally in shell

Is there a linux bash command like the java try catch finally? Or does the linux shell always go on? try { `executeCommandWhichCanFail` mv output } catch { mv log } finally { rm tmp }
Jetse
  • 1,706
  • 2
  • 16
  • 22
96
votes
4 answers

How to create a custom exception and handle it in dart

I have written this code to test how custom exceptions are working in the dart. I'm not getting the desired output could someone explain to me how to handle it?? void main() { try { throwException(); } on customException { …
Vickyonit
  • 1,077
  • 1
  • 7
  • 8
95
votes
5 answers

Use tryCatch skip to next value of loop upon error?

I've read a few other SO questions about tryCatch and cuzzins, as well as the documentation: Exception handling in R catching an error and then branching logic How can I check whether a function call results in a warning? Problems with Plots in…
isomorphismes
  • 8,233
  • 9
  • 59
  • 70
88
votes
2 answers

Try-catch-finally-return clarification

By reading all the questions already asked in this forum related to the topic above (see title), I thoroughly understand that finally gets always called. (except from System.exit and infinite loops). However, I would like to know if a return is…
Rollerball
  • 12,618
  • 23
  • 92
  • 161
87
votes
5 answers

Error object inside catch is of type unknown

I have the following code: try { phpDoc(vscode.window.activeTextEditor); } catch (err) { console.error(err); vscode.window.showErrorMessage(err.message); } however err.message gets the error Object is of type 'unknown'.ts(2571) on err., but I…
Mathias Hillmann
  • 1,537
  • 2
  • 13
  • 25
87
votes
5 answers

php: try-catch not catching all exceptions

I'm trying to do the following: try { // just an example $time = 'wrong datatype'; $timestamp = date("Y-m-d H:i:s", $time); } catch (Exception $e) { return false; } // database activity here In short: I initialize some…
Marcos
  • 1,117
  • 1
  • 8
  • 9
86
votes
4 answers

In Javascript, is it expensive to use try-catch blocks even if an exception is never thrown?

Is it "slow" to use several try-catch blocks when no exceptions are thrown in any of them? My question is the same as this one, but for JavaScript. Suppose I have 20 functions which have try-catch blocks in them and another function that calls every…
cprcrack
  • 17,118
  • 7
  • 88
  • 91
85
votes
11 answers

Does the C# "finally" block ALWAYS execute?

Possible Duplicate: Will code in a Finally statement fire if I return a value in a Try block? Consider the following code C# code. Does the "finally" block execute? public void DoesThisExecute() { string ext = "xlsx"; string message =…
Ryan Rodemoyer
  • 5,548
  • 12
  • 44
  • 54
84
votes
1 answer

Try-With Resource when AutoCloseable is null

How does the try-with feature work for AutoCloseable variables that have been declared null? I assumed this would lead to a null pointer exception when it attempts to invoke close on the variable, but it runs no problem: try (BufferedReader br =…
flakes
  • 21,558
  • 8
  • 41
  • 88
83
votes
11 answers

Throws or try-catch

What is the general rule of thumb when deciding whether to add a throws clause to a method or using a try-catch? From what I've read myself, the throws should be used when the caller has broken their end of the contract (passed object) and the…
James P.
  • 19,313
  • 27
  • 97
  • 155
82
votes
14 answers

Using try/catch for preventing app from crashes

I have been working on an Android app which uses try/catch frequently to prevent it from crashing even on places where there is no need. For example, A view in xml layout with id = toolbar is referenced like: // see new example below, this one is…
mallaudin
  • 4,744
  • 3
  • 36
  • 68
82
votes
13 answers

Is having a return statement just to satisfy syntax bad practice?

Consider the following code: public Object getClone(Cloneable a) throws TotallyFooException { if (a == null) { throw new TotallyFooException(); } else { try { return a.clone(); } catch…
yitzih
  • 3,018
  • 3
  • 27
  • 44
82
votes
14 answers

Check whether a string is parsable into Long without try-catch?

Long.parseLong("string") throws an error if string is not parsable into long. Is there a way to validate the string faster than using try-catch? Thanks
Serg
  • 13,470
  • 8
  • 36
  • 47