Wrapping an exception within another exception in OCaml - exception

Can I make an exception which contains another exception in its constructor? I want to throw an exception about what exception happened lower down.

Yes, like this:
exception Foo
exception Bar of exn
Might get problems with printing.
Fatal error: exception Bar(_)

Related

catching classes that do not inherit from BaseException is not allowed

I'm making a custom plugin to query a database for user info to aide customer support. My backend is slack.
Everytime I start the bot command I'm greeted with:
Computer says nooo. See logs for details:
catching classes that do not inherit from BaseException is not allowed
I'm not sure if this is warning me that I'm attempting to catch an exception that isn't a BaseClass in my code or if an unknown exception was raised and caught elsewhere outside of my plugin.
To debug I tried:
try:
do_the_thing()
except (TypeError, ValueError) as e:
return('Something went wrong.')
I also tried:
try:
do_the_thing()
except Exception as e:
return('Something went wrong.')
And I still get the errbot admonition. Note that the command still runs and does the right thing where there is no exception raised by do_the_thing().
It means that:
Somewhere in your code you have an except ... statement where the exception ... (or one of the exceptions in the sequence ...) is not a subclass of BaseException, and
An exception is being thrown that is caught by that except ... statement.
The TypeError can be raised only when an exception is actually thrown because the names you give to except ... must be evaluated for their current values at that time; just because TypeError referenced a particular class at one point in the program's execution doesn't mean it won't be changed later to reference another object (though that would be admittedly perverse).
The Python interpreter should be giving you a full traceback of the exception; the first thing you need to do is find this. It could be occurring in one of two situations. (This is for single-threaded programs; I'm assuming your program is not multithreaded.)
During the execution of your program, in which case the program will be terminated by the exception, or
During finalization of objects (in their __del__(self) functions) in which case the error will be printed to stderr.
In both cases there should be a stack trace, not just the error message; I've confirmed that at least on Python ≥3.4 a stack trace is printed out for case 2.
You then need to follow this stack trace to see where the problem lies. Remember that the names you give to except ... are variables (even things like TypeError) that can be reassigned, so that you could conceivably be dealing with a (perverse) situation like:
TypeError = False
try:
...
except TypeError:
...
But more likely it will be something obvious such as:
class MyException: # Doesn't inherit from Exception
...
try:
...
except MyException:
...
There is one special case you need to be aware of: if you are seeing messages to stderr (case "2. During finalization," above) printed out as your program exits that means that the exception was thrown during cleanup as the interpreter shuts down, where random variables throughout the program may have already been set to None as part of the cleanup process. But in this case your program should still exit successfully.

Checked vs unchecked exceptions code reusability

I want to know why my program is behaving this way.
I have a method that throws an ArithmeticException when trying to divide by zero. I put this method in a try block. When it throws an exception, if at all, the proceeding catch block will catch this ArithmeticException.
I understand this part 100%.
But I did a bit of experimenting. In my method body:
public static int quotient(int number1, int number2) {
if (number2 == 0)
throw new ArithmeticException("Divisor cannot be zero!");
return number1 / number2;
}
I removed the third line. When I removed the third line, the program still ran fine and performed exactly as it did before. It still caught the ArithmeticException error when it occurred.
Is it because ArithmeticException is an unchecked exception and this error is caught only during runtime, thus negating the need for me to specifically declare that this program will cause an unchecked exception? If it was a checked exception, would I specifically need to declare that this method will throw an unchecked exception?
As you stated Arithmetic exception is a runtime exception, you do not need to specify that it throws an exception.
Although you do need to specify if your program throws a compile time exception using a throws statement. One example where you need to check the exception would be an IOException.

Handle only certain exceptions in Camel sub-route

Thanks to this question I know that I need to specify errorHandler(noErrorHandler()) in a sub-route in order for exceptions to be propagated to the parent route in Camel. However I would like to handle some exceptions in the sub-route but propagate the rest to the parent.
For example:
from("direct:top")
.onException(Exception.class)
.log("Top-level: ${exception}")
.continued(true)
.end()
.to("mock:start")
.to("direct:sub1")
.to("direct:sub2")
.end();
from("direct:sub1")
.errorHandler(noErrorHandler())
.to("mock:sub1")
.throwException(new IllegalArgumentException("test"));
from("direct:sub2")
.onException(IllegalArgumentException.class)
.log("Sub2: ${exception}")
.continued(true)
.end()
// but I want all other exceptions to be propagated to the top handler
.to("mock:sub2")
.throwException(new IllegalArgumentException("test"))
.throwException(new NullPointerException("test"));
sub1 works as expected, but I'd like sub2 to locally handle and log the IllegalArgumentException and allow the NullPointerException to bubble up to the parent. However, my log output looks like this:
10:35:28.048 [main] INFO route1 - Top-level: java.lang.IllegalArgumentException: test
10:35:28.049 [main] INFO route3 - Sub2: java.lang.IllegalArgumentException: test
10:35:28.056 [main] ERROR DefaultErrorHandler - Failed delivery for (MessageId: xxxx on ExchangeId: xxx). Exhausted after delivery attempt: 1 caught: java.lang.NullPointerException: test
If I add errorHandler(noErrorHandler()) to sub2 the IllegalArgumentException is propagated to the parent (which I don't want) and the NullPointerException isn't logged at all. Is there any way to achieve my desired behaviour?
When you throw an exception the exception is cought and route ends immediately, so the second exception can't be thrown. If you want to throw the second exception only if the first one occurs you should throw it from onException block (before .end)

Script#: Support for custom exception classes

In Script#, is it possible to define your own exception classes? I cannot derive from Exception since it's sealed:
public class MyException : Exception {} // Cannot inherit from sealed class Exception
And if I try to throw an exception that doesn't inherit from Exception I get another error:
Error 22 The type caught or thrown must be derived from System.Exception
Seems like there is no support for this?
Its not possible.
You can't catch specific types in generated script. If you've got additional data you'd like to put on the exception, then the Exception type has members to hold on to additional data.

Exception of type 'System.OutOfMemoryException' was thrown

I have an application which throws "Exception of type 'System.OutOfMemoryException'". Which reduces the performance of the server,the CPU usage was around 95%.
I dont know the reason why this problem is occuring ....
can any one pls tell wats the reason for the error and how can i sort it out????
This is my stack trace:
Message
Exception of type 'System.OutOfMemoryException' was thrown.
Source
MySql.Data
Stack
at MySql.Data.MySqlClient.MySqlParameterCollection..ctor() at
MySql.Data.MySqlClient.MySqlCommand..ctor() at
MySql.Data.MySqlClient.MySqlConnection.CreateDbCommand() at
System.Data.Common.DbConnection.System.Data.IDbConnection.CreateCommand() at
IBatisNet.Common.DbProvider.CreateDataParameter() at
IBatisNet.DataMapper.SqlMapSession.CreateDataParameter() at
IBatisNet.DataMapper.Configuration.Statements.PreparedStatementFactory.CreateParametersForTextCommand() at IBatisNet.DataMapper