Questions tagged [exception]

An exception is an unusual condition that requires deviation from the program's normal flow. Normally, an exception should not result in total failure, but instead be attended by an exception handler. Exception handling is a built-in construct in many programming languages. Usually, exceptions are handled by unwinding the stack, thus rolling back to a defined state outside the exception's scope, and then invoking a handler block or routine.

exception
Filter by
Sorted by
Tagged with
361 votes
25 answers
1.0m views

What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?

What does ArrayIndexOutOfBoundsException mean and how do I get rid of it? Here is a code sample that triggers the exception: String[] names = { "tom", "bob", "harry" }; for (int i = 0; i <= ...
Aaron's user avatar
  • 11.5k
54 votes
6 answers
87k views

I'm getting an IndentationError (or a TabError). How do I fix it?

I have a Python script: if True: if False: print('foo') print('bar') However, when I attempt to run my script, Python raises an IndentationError: File "script.py", line 4 ...
Christian Dean's user avatar
501 votes
19 answers
642k views

What does "Fatal error: Unexpectedly found nil while unwrapping an Optional value" mean?

My Swift program is crashing with EXC_BAD_INSTRUCTION and one of the following similar errors. What does this error mean, and how do I fix it? Fatal error: Unexpectedly found nil while unwrapping ...
533 votes
15 answers
858k views

What is a StackOverflowError?

What is a StackOverflowError, what causes it, and how should I deal with them?
Ziggy's user avatar
  • 22.1k
763 votes
21 answers
348k views

Understanding checked vs unchecked exceptions in Java

Joshua Bloch in "Effective Java" said that Use checked exceptions for recoverable conditions and runtime exceptions for programming errors (Item 58 in 2nd edition) Let's see if I understand ...
Thang Pham's user avatar
  • 38.5k
302 votes
34 answers
1.7m views

How can I solve "java.lang.NoClassDefFoundError"?

I've tried both the examples in Oracle's Java Tutorials. They both compile fine, but at run time, both come up with this error: Exception in thread "main" java.lang.NoClassDefFoundError: ...
Jonathan Lam's user avatar
  • 17.1k
79 votes
5 answers
25k views

VS2010 does not show unhandled exception message in a WinForms Application on a 64-bit version of Windows

When I create a new project, I get a strange behavior for unhandled exceptions. This is how I can reproduce the problem: 1) create a new Windows Forms Application (C#, .NET Framework 4, VS2010) 2) ...
Robert Hegner's user avatar
226 votes
24 answers
64k views

Why not use exceptions as regular flow of control?

To avoid all standard-answers I could have Googled on, I will provide an example you all can attack at will. C# and Java (and too many others) have with plenty of types some of ‘overflow’ behaviour I ...
Peter's user avatar
  • 48.5k
14 votes
4 answers
12k views

Android Kotlin: Getting a FileNotFoundException with filename chosen from file picker?

I'm working on an Android application where one of the features is to let the user choose a file to open (I'm wanting to open a plain text .txt file). I've worked on Android apps before with Java, ...
CalicoSkies's user avatar
174 votes
8 answers
227k views

Why is a ConcurrentModificationException thrown and how to debug it

I am using a Collection (a HashMap used indirectly by the JPA, it so happens), but apparently randomly the code throws a ConcurrentModificationException. What is causing it and how do I fix this ...
mainstringargs's user avatar
531 votes
6 answers
557k views

How to use the tryCatch() function?

I want to write code using tryCatch to deal with errors downloading data from the web. url <- c( "http://stat.ethz.ch/R-manual/R-devel/library/base/html/connections.html", "...
Dd Pp's user avatar
  • 5,847
5658 votes
27 answers
3.7m views

How do I create a directory, and any missing parent directories?

How do I create a directory at a given path, and also create any missing parent directories along that path? For example, the Bash command mkdir -p /path/to/nested/directory does this.
Parand's user avatar
  • 105k
418 votes
19 answers
342k views

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 ...
Vader's user avatar
  • 6,556
171 votes
14 answers
419k views

Official reasons for "Software caused connection abort: socket write error"

Given this stack trace snippet Caused by: java.net.SocketException: Software caused connection abort: socket write error  at java.net.SocketOutputStream.socketWrite0(Native Method) I ...
Eran Medan's user avatar
  • 45.3k
1242 votes
34 answers
1.1m views

Dealing with "java.lang.OutOfMemoryError: PermGen space" error

Recently I ran into this error in my web application: java.lang.OutOfMemoryError: PermGen space It's a typical Hibernate/JPA + IceFaces/JSF application running on Tomcat 6 and JDK 1.6. Apparently ...
253 votes
10 answers
720k views

Why does this iterative list-growing code give IndexError: list assignment index out of range? How can I repeatedly add (append) elements to a list?

I tried writing some code like: i = [1, 2, 3, 5, 8, 13] j = [] k = 0 for l in i: j[k] = l k += 1 But I get an error message that says IndexError: list assignment index out of range, ...
Vladan's user avatar
  • 2,853
207 votes
27 answers
331k views

Causes of getting a java.lang.VerifyError

I'm investigating the following java.lang.VerifyError: java.lang.VerifyError: (class: be/post/ehr/wfm/application/serviceorganization/report/DisplayReportServlet, method: getMonthData signature: (...
Jeroen Wyseur's user avatar
2297 votes
35 answers
1.9m views

How do you assert that a certain exception is thrown in JUnit tests?

How can I use JUnit idiomatically to test that some code throws an exception? While I can certainly do something like this: @Test public void testFooThrowsIndexOutOfBoundsException() { boolean ...
SCdF's user avatar
  • 58.4k
983 votes
6 answers
352k views

Why is it bad style to `rescue Exception => e` in Ruby?

Ryan Davis’s Ruby QuickRef says (without explanation): Don’t rescue Exception. EVER. or I will stab you. Why not? What’s the right thing to do?
John's user avatar
  • 29.5k
84 votes
2 answers
36k views

Deciphering the .NET clr20r3 exception parameters P1..P10

I'm trying to decipher the meaning on the P1...P10 parameters associated with a clr20r3 that is written to the event log when my application experiences an exception. The best I've been able to find ...
Ian Boyd's user avatar
  • 252k
320 votes
18 answers
155k views

If you shouldn't throw exceptions in a destructor, how do you handle errors in it?

Most people say never throw an exception out of a destructor - doing so results in undefined behavior. Stroustrup makes the point that "the vector destructor explicitly invokes the destructor for ...
Greg Rogers's user avatar
  • 36.1k
506 votes
33 answers
185k views

When to throw an exception?

I have exceptions created for every condition that my application does not expect. UserNameNotValidException, PasswordNotCorrectException etc. However I was told I should not create exceptions for ...
3233 votes
11 answers
3.1m views

Manually raising (throwing) an exception in Python

How do I raise an exception in Python so that it can later be caught via an except block?
TIMEX's user avatar
  • 266k
568 votes
18 answers
107k views

What are the effects of exceptions on performance in Java?

Question: Is exception handling in Java actually slow? Conventional wisdom, as well as a lot of Google results, says that exceptional logic shouldn't be used for normal program flow in Java. Two ...
John Ellinwood's user avatar
560 votes
13 answers
285k views

Is there a difference between "throw" and "throw ex"?

There are some posts that asks what the difference between those two are already. (why do I have to even mention this...) But my question is different in a way that I am calling "throw ex" in another ...
dance2die's user avatar
  • 36.5k
1380 votes
16 answers
1.8m views

How do I check if a variable exists?

I want to check if a variable exists. Now I'm doing something like this: try: myVar except NameError: # Do something. Are there other ways without exceptions?
Max Frai's user avatar
  • 63.2k
3831 votes
6 answers
1.5m views

How to catch multiple exceptions in one line? (in the "except" block)

I know that I can do: try: # do something that may fail except: # do this if ANYTHING goes wrong I can also do this: try: # do something that may fail except IDontLikeYouException: # ...
inspectorG4dget's user avatar
950 votes
12 answers
1.2m views

How to properly ignore exceptions

When you just want to do a try-except without handling the exception, how do you do it in Python? Is the following the right way to do it? try: shutil.rmtree(path) except: pass
Joan Venge's user avatar
  • 324k
329 votes
45 answers
267k views

What to do on TransactionTooLargeException

I got a TransactionTooLargeException. Not reproducible. In the docs it says The Binder transaction failed because it was too large. During a remote procedure call, the arguments and the return value ...
User's user avatar
  • 32.1k
114 votes
9 answers
988k views

Input string was not in a correct format

I'm new with C#, I have some basic knowledge in Java but I can't get this code to run properly. It's just a basic calculator, but when I run the program VS2008 gives me this error: I did almost the ...
kustomrtr's user avatar
  • 1,447
809 votes
21 answers
1.8m views

jQuery Ajax error handling, show custom exception messages

Is there some way I can show custom exception messages as an alert in my jQuery AJAX error message? For example, if I want to throw an exception on the server side via Struts by throw new ...
user avatar
21 votes
2 answers
11k views

Can't get ArrayIndexOutOfBoundsException from Future<?> and SwingWorker if thread starts Executor

I play with multitreading for SwingWorker by using Executor, and I'm there by mistake identified wrong elements from the Vector, looks like as this code pretty ignores that element in Vector doesn't ...
mKorbel's user avatar
  • 110k
15 votes
5 answers
98k views

Unable to use Selenium Webdriver. Getting two exceptions

I am getting the following error when trying to create an object with Selenium Webdriver. "\selenium\webdriver\common\driver_finder.py", line 42, in get_path path = SeleniumManager()....
startrek-07's user avatar
1871 votes
17 answers
1.2m views

Proper way to declare custom exceptions in modern Python?

What's the proper way to declare custom exception classes in modern Python? My primary goal is to follow whatever standard other exception classes have, so that (for instance) any extra string I ...
Nelson's user avatar
  • 28.7k
11 votes
2 answers
6k views

Better exception for non-exhaustive patterns in case

Is there a way to get GHCi to produce better exception messages when it finds at runtime that a call has produced value that does not match the function's pattern matching? It currently gives the ...
toofarsideways's user avatar
1151 votes
10 answers
1.2m views

How can I write a `try`/`except` block that catches all exceptions?

How can I write a try/except block that catches all exceptions?
user469652's user avatar
  • 50.2k
132 votes
12 answers
240k views

How to use wait and notify in Java without IllegalMonitorStateException?

I have 2 matrices and I need to multiply them and then print the results of each cell. As soon as one cell is ready I need to print it, but for example I need to print the [0][0] cell before cell [2][...
user avatar
829 votes
11 answers
595k views

Node.js Best Practice Exception Handling

I just started trying out node.js a few days ago. I've realized that the Node is terminated whenever I have an unhandled exception in my program. This is different than the normal server container ...
momo's user avatar
  • 21.3k
623 votes
17 answers
649k views

Why do I get an UnsupportedOperationException when trying to remove an element from a List?

I have this code: public static String SelectRandomFromTemplate(String template,int count) { String[] split = template.split("|"); List<String> list=Arrays.asList(split); Random r = ...
Pentium10's user avatar
  • 206k
7 votes
2 answers
2k views

How to generate exceptions from RepaintManager

In connection with my question (may be), I found another exception type that I not able to catch and print-out from SwingWorker thread. How can I to generate RepaintManager exceptions? I read this ...
mKorbel's user avatar
  • 110k
1466 votes
19 answers
1.5m views

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 ...
chriscauley's user avatar
  • 20.1k
212 votes
5 answers
190k views

How to handle AccessViolationException

I am using a COM object (MODI) from within my .net application. The method I am calling throws a System.AccessViolationException, which is intercepted by Visual Studio. The odd thing is that I have ...
Jeremy's user avatar
  • 45.6k
371 votes
10 answers
752k views

Stack smashing detected

I am executing my a.out file. After execution the program runs for some time then exits with the message: **** stack smashing detected ***: ./a.out terminated* *======= Backtrace: =========* */lib/...
Biswajyoti Das's user avatar
269 votes
18 answers
73k views

When to choose checked and unchecked exceptions

In Java (or any other language with checked exceptions), when creating your own exception class, how do you decide whether it should be checked or unchecked? My instinct is to say that a checked ...
Matt Sheppard's user avatar
141 votes
4 answers
386k views

How can I read a text file in Android? [duplicate]

I want to read the text from a text file. In the code below, an exception occurs (that means it goes to the catch block). I put the text file in the application folder. Where should I put this text ...
user1635224's user avatar
  • 1,643
50 votes
6 answers
212k views

Why do I get "Exception; must be caught or declared to be thrown" when I try to compile my Java code?

Consider: import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.crypto.*; import javax.crypto.spec.*; import java.security.*; import java.io.*; public class EncryptURL ...
mmundiff's user avatar
  • 3,935
310 votes
11 answers
194k views

Best practices for catching and re-throwing .NET exceptions

What are the best practices to consider when catching exceptions and re-throwing them? I want to make sure that the Exception object's InnerException and stack trace are preserved. Is there a ...
Seibar's user avatar
  • 69.5k
94 votes
10 answers
47k views

How expensive are exceptions in C#?

How expensive are exceptions in C#? It seems like they are not incredibly expensive as long as the stack is not deep; however I have read conflicting reports. Is there definitive report that hasn't ...
Chance's user avatar
  • 11.2k
741 votes
4 answers
987k views

Correct way to try/except using Python requests module?

try: r = requests.get(url, params={'s': thing}) except requests.ConnectionError, e: print(e) Is this correct? Is there a better way to structure this? Will this cover all my bases?
John Smith's user avatar
379 votes
9 answers
186k views

WPF global exception handler [duplicate]

Sometimes, under not reproducible circumstances, my WPF application crashes without any message. The application simply close instantly. Where is the best place to implement the global Try/Catch ...
Scott Olson's user avatar
  • 4,139

1
2 3 4 5
141