How-to articles, tricks, and solutions about EXCEPTION

Catch multiple exceptions in one line (except block)

In Python, you can catch multiple exceptions in a single except block by separating the exceptions with a tuple.

How can I handle the warning of file_get_contents() function in PHP?

The file_get_contents() function in PHP is used to read a file and return its contents as a string.

How can I safely create a nested directory?

To safely create a nested directory in python, you can use the os module and the makedirs function.

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

java.lang.NoClassDefFoundError is an error that occurs when the Java Virtual Machine (JVM) can't find a required class definition at runtime. This can happen for a variety of reasons, including:

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

You can catch all exceptions by using the Exception class in the except block, like this:

How do I check if a variable exists?

In Python, you can check if a variable exists by using the globals() or locals() function to check if the variable is in the global or local namespace, respectively.

How do I print an exception in Python?

You can print an exception in Python by using the print() function and passing the exc variable which is the default variable name for an exception.

How do you test that a Python function throws an exception?

You can use the pytest.raises function to test that a Python function throws an exception.

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

To assert that a certain exception is thrown in a JUnit test, you can use the @Test annotation's expected attribute. Here's an example:

How to catch and print the full exception traceback without halting/exiting the program?

You can use the traceback module to catch and print the full exception traceback without halting/exiting the program.

How to properly ignore exceptions

It's generally not recommended to ignore exceptions, as they often indicate a problem in the code that should be addressed.

How to test that no exception is thrown?

To test that no exception is thrown in a Java method, you can use the assertDoesNotThrow method from the org.junit.jupiter.api.Assertions class (part of the JUnit 5 library).

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)

A java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES) error typically indicates that the MySQL server is unable to authenticate the user with the provided username and password.

Manually raising (throwing) an exception in Python

In Python, you can raise an exception using the raise statement.

Proper way to declare custom exceptions in modern Python?

To declare a custom exception in Python, you can create a new class that inherits from the built-in Exception class.

python exception message capturing

To capture an exception message in Python, you can use a try-except block and the as keyword to assign the exception message to a variable.

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

java.lang.ArrayIndexOutOfBoundsException is an exception that is thrown when you try to access an element of an array with an index that is out of bounds. This can happen if you try to access an index that is either negative or greater than or equal to

What could cause java.lang.reflect.InvocationTargetException?

The java.lang.reflect.InvocationTargetException is a checked exception that is thrown when an exception is thrown by an invoked method or constructor.

What is a StackOverflowError?

A StackOverflowError is an error that occurs when the Java Virtual Machine (JVM) runs out of space on the call stack. The call stack is a data structure that is used to store information about method calls, including the name of the method, the parameters

What is the use of "assert" in Python?

In Python, the assert statement is used to check if a certain condition is true, and if it is not true, raise an exception.

Which exception should I raise on bad/illegal argument combinations in Python?

You can raise the ValueError exception when you encounter bad or illegal argument combinations in Python.

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

The java.lang.UnsupportedOperationException is thrown when an operation is not supported by a class.