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
1620
votes
6 answers

Try-catch speeding up my code?

I wrote some code for testing the impact of try-catch, but seeing some surprising results. static void Main(string[] args) { Thread.CurrentThread.Priority = ThreadPriority.Highest; Process.GetCurrentProcess().PriorityClass =…
Eren Ersönmez
  • 38,383
  • 7
  • 71
  • 92
1372
votes
19 answers

Catch and print full Python exception traceback without halting/exiting the program

I want to catch and log exceptions without exiting, e.g., try: do_stuff() except Exception as err: print(Exception, err) # I want to print the entire traceback here, # not just the exception name and details I want to print the…
chriscauley
  • 19,015
  • 9
  • 33
  • 33
850
votes
11 answers

Can I catch multiple Java exceptions in the same catch clause?

In Java, I want to do something like this: try { ... } catch (/* code to catch IllegalArgumentException, SecurityException, IllegalAccessException, and NoSuchFieldException at the same time */) { someCode(); } ...instead…
froadie
  • 79,995
  • 75
  • 166
  • 235
708
votes
18 answers

Why catch and rethrow an exception in C#?

I'm looking at the article C# - Data Transfer Object on serializable DTOs. The article includes this piece of code: public static string SerializeDTO(DTO dto) { try { XmlSerializer xmlSer = new XmlSerializer(dto.GetType()); …
corlettk
  • 13,288
  • 7
  • 38
  • 52
559
votes
11 answers

Is it a good practice to use try-except-else in Python?

From time to time in Python, I see the block: try: try_this(whatever) except SomeException as exception: #Handle exception else: return something What is the reason for the try-except-else to exist? I do not like that kind of programming,…
Juan Antonio Gomez Moriano
  • 13,103
  • 10
  • 47
  • 65
519
votes
6 answers

How to write trycatch in R

I want to write trycatch code to deal with error in downloading from the web. url <- c( "http://stat.ethz.ch/R-manual/R-devel/library/base/html/connections.html", "http://en.wikipedia.org/wiki/Xz") y <- mapply(readLines, con=url) These two…
Dd Pp
  • 5,727
  • 4
  • 21
  • 19
475
votes
16 answers

Why should I not wrap every block in "try"-"catch"?

I have always been of the belief that if a method can throw an exception then it is reckless not to protect this call with a meaningful try block. I just posted 'You should ALWAYS wrap calls that can throw in try, catch blocks.' to this question and…
Konrad
  • 39,751
  • 32
  • 78
  • 114
414
votes
13 answers

Can I try/catch a warning?

I need to catch some warnings being thrown from some php native functions and then handle them. Specifically: array dns_get_record ( string $hostname [, int $type= DNS_ANY [, array &$authns [, array &$addtl ]]] ) It throws a warning when the…
user121196
  • 30,032
  • 57
  • 148
  • 198
413
votes
19 answers

Why is "except: pass" a bad programming practice?

I often see comments on other Stack Overflow questions about how the use of except: pass is discouraged. Why is this bad? Sometimes I just don't care what the errors are and I want to just continue with the code. try: something except: …
Vader
  • 6,335
  • 8
  • 31
  • 43
335
votes
13 answers

Do try/catch blocks hurt performance when exceptions are not thrown?

During a code review with a Microsoft employee we came across a large section of code inside a try{} block. She and an IT representative suggested this can have effects on performance of the code. In fact, they suggested most of the code should be…
Kobi
  • 135,331
  • 41
  • 252
  • 292
258
votes
12 answers

Will code in a Finally statement fire if I return a value in a Try block?

I'm reviewing some code for a friend and say that he was using a return statement inside of a try-finally block. Does the code in the Finally section still fire even though the rest of the try block doesn't? Example: public bool someMethod() { …
JamesEggers
  • 12,885
  • 14
  • 59
  • 86
258
votes
29 answers

How do you implement a re-try-catch?

Try-catch is meant to help in the exception handling. This means somehow that it will help our system to be more robust: try to recover from an unexpected event. We suspect something might happen when executing and instruction (sending a message),…
Andres Farias
  • 2,683
  • 2
  • 13
  • 9
228
votes
9 answers

try/catch + using, right syntax

Which one: using (var myObject = new MyClass()) { try { // something here... } catch(Exception ex) { // Handle exception } } OR try { using (var myObject = new MyClass()) { // something here... …
Xaqron
  • 29,931
  • 42
  • 140
  • 205
216
votes
7 answers

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

We know that it is expensive to catch exceptions. But, is it also expensive to use a try-catch block in Java even if an exception is never thrown? I found the Stack Overflow question/answer Why are try blocks expensive?, but it is for .NET.
jsedano
  • 4,088
  • 2
  • 20
  • 31
213
votes
20 answers

Why are empty catch blocks a bad idea?

I've just seen a question on try-catch, which people (including Jon Skeet) say empty catch blocks are a really bad idea? Why this? Is there no situation where an empty catch is not a wrong design decision? I mean, for instance, sometimes you want to…
Samuel Carrijo
  • 17,449
  • 12
  • 49
  • 59
1
2 3
99 100