exception-handling

Rails functional test not catching/getting exceptions

When running one of my app's functional tests the other day I hit a bug in my code that was causing a RoutingError. My functional test was loading a page and in that page it was creating a link to a page that had no valid route to it. The specific cause of the exception is not the point though - the problem was that this exception was ...

How do I find the inner-most exception without using a while loop?

When C# throws an exception, it can have an inner exception. What I want to do is get the inner-most exception, or in other words, the leaf exception that doesn't have an inner exception. I can do this in a while loop: while (e.InnerException != null) { e = e.InnerException; } But I was wondering if there was some one-liner I coul...

Testing in Python - how to use assertRaises in testing using unittest?

Hi, I am trying to do a simple test in Python using unittest, to see if a class throws an exception if it gets an unsuitable input for the constructor. The class looks like this: class SummaryFormula: def __init__( self, summaryFormula): self.atoms = {} for atom in re.finditer( "([A-Z][a-z]{0,2})(\d*)", summaryForm...

How to check that a regular expression has matched a string completely, i.e. - the string did not contain any extra character?

I have two questions: 1) I have a regular expression ([A-Z][a-z]{0,2})(\d*) and I am using Python's re.finditer() to match appropriate strings. My problem is, that I want to match only strings that contain no extra characters, otherwise I want to raise an exception. I want to catch a following pattern: - capital letter, followed by 0,...

closing files properly opened with urllib2.urlopen()

I have following code in a python script try: # send the query request sf = urllib2.urlopen(search_query) search_soup = BeautifulSoup.BeautifulStoneSoup(sf.read()) sf.close() except Exception, err: print("Couldn't get programme information.") print(str(err)) return I'm concerned because if I encounter a...

Right way to close WPF GUI application: GetCurrentProcess().Kill(), Environment.Exit(0) or this.Shutdown()

My GUI desktop-based WPF 4.0 (C# .Net 4.0) program works with SQL Server database. Each time when I run my application it creates connection to SQL Server via ADO.NET Entity Framework and if SQL Server is not reachable it throws exception and shows MessageBox with notification. Now I want that after user read this message application wi...

How to tell apart different exception types in BackgroundWorker.RunWorkerCompleted event handler

I am doing little hobby project in C#, a language I do not know well, and have stumbled upon the following: Suppose you have an asynchronous operation implemented by using BackgroundWorker. Now if there is an exception, event RunWorkerCompleted will be raised and RunWorkerCompletedEventArgs.Error will be non-null. Is the following the ...

Eating Exceptions in c# om nom nom

Given that eating exceptions is always bad juju and re-throwing the exception loses the call stack, what's the proper way to re-factor the following? Eating Exceptions: try { … do something meaningful } catch(SomeException ex) { // eat exception } ...

unreported exception java.io.IOException

What's wrong with this code import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; /** * * @author Master */ public class Server { try { ServerSocket S = new ServerSocket(3333); Socket So = S.accept(); } catch(IOException e) { System.out.println("IOError"); } } Fi...

put an exception to an exception to get the next exception on .net

I use thrown in visual studio 2005 while debugging a project to get the possible error. But when I do it, it stops with an green arrow pointing the error line that is happening, but I want to skip that error to get the next possible error. How can I skip and set the yellow arrow to make it yellow and let it to go on to the part of th...

function try block. An interesting example

Hello all, Consider the following C++ program struct str { int mem; str() try :mem(0) { throw 0; } catch(...) { } }; int main() { str inst; } The catch block works, i.e. the control reaches it, and then the program crashes. I can't understand what...

Function try blocks, but not in constructors

Hello, just a quick question. Is there any difference between void f(Foo x) try { ... } catch(exception& e) { ... } and void f(Foo x) { try { ... } catch (exception& e) { ... } } ? If no, why are function try blocks for (the case of initialization lists for constructors being put aside) ? What happens...

How to raise a warning in Python without stopping (interrupting) the program?

Hi, I am dealing with a problem how to raise a Warning in Python without having to let the program crash / stop / interrupt. I use following simple function that only checks if the user passed to it a non-zero number. If the user passes a zero, the program should warn the user, but continue normally. It should work like the following co...

Is there a way to generically wrap any function call in a try/catch block?

I am writing a bunch of integration tests for a project. I want to call each individual integration point method wrapped in a try/catch block so that when it fails, I get some sort of feedback to display, rather than just crashing the app. I also want to be able to time how long the calls take, and check return values when needed. So, I ...

How to test with Python's unittest that a warning has been thrown?

Hi, I have a following function in Python and I want to test with unittest that if the function gets 0 as argument, it throws a warning. I already tried assertRaises, but since I don't raise the warning, that doesn't work. def isZero( i): if i != 0: print "OK" else: warning = Warning( "the input is 0!...

C# declare a method always throws an exception?

I have a method like... int f() { try { int i = process(); return i; } catch(Exception ex) { ThrowSpecificFault(ex); } } This produces a compiler error, "not all code paths return a value". But in my case ThrowSpecificFault() will always throw (the appropriate) exception. So I am forced to a put a return value at t...

Testing for Exceptions using JUnit. Test fails even if the Exception is caught.

Hi, I am new to testing with JUnit and I need a hint on testing Exceptions. I have a simple method that throws an exception if it gets an empty input string: public SumarniVzorec( String sumarniVzorec) throws IOException { if (sumarniVzorec == "") { IOException emptyString = new IOException("The input...

private inheritance, friends, and exception-handling

Hi. When class A privately inherits from class B it means that B is a private base class subobject of A. But not for friends, for friends it is a public sububject. And when there are multiple catch handlers the first one that matches (that is, if the exception type can be implicitly converted to the handler's parameter type) is called. S...

Can I prevent an uncaught exception in another AppDomain from shutting down the application ?

Hello, I'm having trouble with a misbehaved library that throws an exception in a finalizer, which of course crashes the application. To avoid this, I tried loading the library in its own AppDomain, but the exception still bubbles to the surface and crashes the application. As documented on MSDN, registering to AppDomain.UnhandledExce...

FileOutputStream and exception handling

I'm trying to run this piece of code inside my onCreate method as an initial test into writing private data for my app to use. This code is straight out of the Android SDK development guide located here (http://developer.android.com/guide/topics/data/data-storage.html#filesInternal) String FILENAME = "hello_file"; String string = "hello...