How to print the stack trace of an exception object in Python?

It’s a bit inconvenient, but you can use traceback.print_exception. Given an exception ex: traceback.print_exception(type(ex), ex, ex.__traceback__) Example: import traceback try: 1/0 except Exception as ex: traceback.print_exception(type(ex), ex, ex.__traceback__) # output: # Traceback (most recent call last): # File “untitled.py”, line 4, in <module> # 1/0 # ZeroDivisionError: division by zero

How to return a value from try, catch, and finally?

To return a value when using try/catch you can use a temporary variable, e.g. public static double add(String[] values) { double sum = 0.0; try { int length = values.length; double arrayValues[] = new double[length]; for(int i = 0; i < length; i++) { arrayValues[i] = Double.parseDouble(values[i]); sum += arrayValues[i]; } } catch(NumberFormatException e) { … Read more

Why do I need to handle an exception for Thread.sleep()?

If a method is declared in a way that it can throw checked exceptions (Exceptions that are not subclasses of RuntimeException), the code that calls it must call it in a try-catch block or the caller method must declare to throw it. Thread.sleep() is declared like this: public static void sleep(long millis) throws InterruptedException; It … Read more

Try-catch-finally and then again a try catch

Write a SQLUtils class that contains static closeQuietly methods that catch and log such exceptions, then use as appropriate. You’ll end up with something that reads like this: public class SQLUtils { private static Log log = LogFactory.getLog(SQLUtils.class); public static void closeQuietly(Connection connection) { try { if (connection != null) { connection.close(); } } catch … Read more

How to handle constructor failure for RAII

The problem is that your class is trying to do too much. The principle of RAII is that it acquires a resource (either in the constructor, or later), and the destructor releases it; the class exists solely to manage that resource. In your case, anything other than DoSomething() and UndoSomething() should be the responsibility of … Read more

Python urllib2 URLError HTTP status code.

You shouldn’t check for a status code after catching URLError, since that exception can be raised in situations where there’s no HTTP status code available, for example when you’re getting connection refused errors. Use HTTPError to check for HTTP specific errors, and then use URLError to check for other problems: try: urllib2.urlopen(url) except urllib2.HTTPError, e: … Read more

What’s better to use, a __try/__except block or a try / catch block?

They are two very different things. try/catch are the familiar C++ keywords you know. __try/__except is used to catch SEH exceptions. Exceptions raised by Windows itself, like DivisionByZero or AccessViolation. It is well described in the MSDN Library article for it. You can also use it to catch C++ exception because it leverages the Windows … Read more

Mockito throw Exception

Change this: thenThrow(DataAccessException.class) to thenThrow(new DataAccessException(“…”){ }) Example: when(userSubModuleDao.findById(any(UserSubModuleId.class),eq(false))).thenThrow(new DataAccessException(“…”){}); You can only pass a Class reference when that Exception type has a No-Arg constructor, and the Spring exception does not have one.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)