#PreUpdate doesn't return correct exception message - jpa-2.0

To validate my entity beans I have created this validation method inside my bean
#PrePersist
#PreUpdate
public void validate(){
if (startTime.after(stopTime)) throw new ValidationException("Wrong order");
}
The ValidationException looks like this:
#ApplicationException(rollback = true)
public class ValidationException extends RuntimeException {
public ValidationException(String message) {
super(message);
}
}
If I try to create a bean with
try {
timeframefacade.create(timeframe);
} catch (Exception e) {
System.out.println(e.getMessage());
e.getMessage() returns "Wrong order", which is correct
try {
timeframefacade.edit(timeframe);
} catch (Exception e) {
System.out.println(e.getMessage());
it returns "Transaction aborted" which is an EJBException. How do I get the same result as above and avoid all the logging, which also occurs?

Call flush within your edit method to force the preupdate. Otherwise it occurs when the transaction is completing and the container must throw a transaction exception instead.

Related

What happens if inputStream is null for a AWS lambda?

So, I have to investigate if null InputStream goes to catch block of the code
Example code snippet::
public class TestLambda implements RequestStreamHandler {
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) {
try {
if (inputStream != null && inputStream.available() != 0) {
System.out.println("do something...");
}
} catch (Exception exception) {
System.out.println("in catch block");
}
My question is, if I do not put an else condition to the if block above and there is null inputStream, should the control go into catch block?
No, control won't enter the exception block unless an Exception is thrown. Just having a null inputStream alone will not cause an exception to be thrown because the if condition is checking if it is null.

How to use an exception outside catch statement

I have a function that may throw an exception of a variable type, but all inheriting from std::exception:
int errorCheck(int a)
{
if(a < 0)
throw a_too_small();
if(a>10)
throw a_too_big();
return a*2;
}
I want to handle this exception using Qt's SIGNAL/SLOT mechanism.
try {
errorCheck(a);
} catch(std::exception &e) {
emit errorSignal(e);
}
If the signature of errorSignal is errorSignal(std::exception e), then the original exception object is sliced and its type is lost.
If the signature is errorSignal(std::exception &e) on the other hand, the exception e leaves the catch scope, and I believe this is not allowed.
What's the cleanest solution to handle this problem?
If using C++11 is possible, you can try to use std::exception_ptr:
try {
...
} catch (...)
std::exception_ptr except = std::current_exception();
emit signal(except);
}
Then in your slot:
void myslot(std::exception_ptr e) {
try {
if (e) {
std::rethrow_exception(e);
}
} catch (...) {
...
}
}
How about making the errorSignal function signal/slot work by reference? something like errorSignal(std::exception& e)? Do that with Qt::DirectConnection too to avoid event queuing, with something like:
connect(someWidget,SomeWidget::errorSignal&,this,MainWindow::handleError&,Qt::DirectConnection);

Why can I not catch my exception?

I have following function that catches the system-exception of a ComObject and throws my own exception off:
int TReader::ExecSQL(...) {
try {
// ...
} catch (Comobj::EOleException& e) {
throw myDBError("TReader::Open", "TReader", e.Message);
}
// ...
}
I can not catch my own exception, getting always "Exception unknown!"! Why?
void main() {
try {
ExecSQL(...);
} catch(myDBError& e) {
log(e.Message);
} catch(...) {
log("Exception unknown!");
}
}
Use catch(const myDBError& e) { instead.
An anonymous temporary cannot bind to a non-const reference, so a throw will not be intercepted at the current catch site.
If try-catch block generates not Comobj::EOleException, but something else, that may be problem.
Also check, maybe myDBError constructor generates another exception, with another type :-)
That would be funniest explanation, why you can't catch myDBError, but still catch something :-)

How to add info to a std::exception using boost exception

The Boost Exception framework is great. You can add information to an exception derived from std::exception and boost::exception at the appropriate level of an application, as described in the documentation.
But how can such information be added if you do not control the throw site in the code, e.g. the std lib throws an exception, e.g. map throws out_of_range?
It cannot be caught as boost::exception, because it does not derive from it:
try {
my_map.at(id);
} catch(boost::exception &e) { // NOT caught
e << errinfo_desc("id not found, map out of range");
throw;
}
It can be caught as std::exception because out_of_range derives from std::exception, but then no information can be added because it not a boost::exception:
try {
my_map.at(id);
} catch(std::exception &e) {
// compile error: e << errinfo_desc("id not found, map out of range");
throw;
}
Catching a std::exception and throwing a new boost::exception loses the original location of the exception which is not desired:
try {
my_map.at(id);
} catch(std::exception &e) {
BOOST_THROW_EXCEPTION(my_exception()
<< errinfo_desc("id not found, map out of range"));
}
Is is possible to preserve the original exception with its location etc. and still be able to add more information later? How?
Personally, I use the std::exception hierarchy wherever possible. And when they are not sufficient, then I just derive my exception class from std::exception. Never had any use case for boost::exception (perhaps all my code is too simple for that, don't know).
But if you really want to combine both, here is a wild idea: use multiple inheritance. Code sketch (untested, more like pseudocode):
// two personalities in one exception object
class MyException: public std::exception, public boost::exception {
explicit MyException(const std::exception& stdex) { ... }
... other ctors as needed...
... other stuff ...
};
...
try {
my_map.at(id);
} catch (const std::exception& stdex) {
MyException myex(stdex); // gets SOME of the std::exception information
myex << errinfo_desc("id not found, map out of range"); // boost::exception goodies
throw myex;
}
Obvious caveat: slicing issues when initialising MyException objects with a std::exception reference. And the usual problems with MI. You have been warned :-)
You can do this by catching both cases: boost::exception and std::exception
try {
my_map.at(id);
} catch(boost::exception &e) {
// If already a boost exception, catch here and add information
e << errinfo_desc("id not found, map out of range");
throw;
} catch(std::exception &) {
// Otherwise, catch here and convert to a boost exception
try { boost::rethrow_exception( boost::current_exception() ); }
catch( boost::exception& e ) {
e << errinfo_desc("id not found, map out of range");
throw;
}
}
If find it's a pain to write that much code, so I asked if there was a cleaner way of doing this: Adding error_info to std::exception

Which is correct? catch (_com_error e) or catch (_com_error& e)?

Which one should I use?
catch (_com_error e)
or
catch (_com_error& e)
The second. Here is my attempt at quoting Sutter
"Throw by value, catch by reference"
Learn to catch properly: Throw exceptions by value (not pointer) and
catch them by reference (usually to const). This is the combination
that meshes best with exception semantics. When rethrowing the same
exception, prefer just throw; to throw e;.
Here's the full Item 73. Throw by value, catch by reference.
The reason to avoid catching exceptions by value is that it implicitly makes a copy of the exception. If the exception is of a subclass, then information about it will be lost.
try { throw MyException ("error") }
catch (Exception e) {
/* Implies: Exception e (MyException ("error")) */
/* e is an instance of Exception, but not MyException */
}
Catching by reference avoids this issue by not copying the exception.
try { throw MyException ("error") }
catch (Exception& e) {
/* Implies: Exception &e = MyException ("error"); */
/* e is an instance of MyException */
}
Personally, I would go for the third option:
catch (const _com_error& e)
Also, note that, when using MFC, you may have to catch by pointer. Otherwise, #JaredPar's answer is the way you should normally go (and hopefully never have to deal with things that throw a pointer).
Definitely the second. If you had the following:
class my_exception : public exception
{
int my_exception_data;
};
void foo()
{
throw my_exception;
}
void bar()
{
try
{
foo();
}
catch (exception e)
{
// e is "sliced off" - you lose the "my_exception-ness" of the exception object
}
}