I am working on a legacy grails 2.4.x app.
I wanted to add some extra exception visibility to a class that extends ThreadPoolExecutor
As per
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ThreadPoolExecutor.html#afterExecute-java.lang.Runnable-java.lang.Throwable-
I implemented
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
if (t == null && r instanceof Future<?>) {
try {
Object result = ((Future<?>) r).get();
} catch (CancellationException ce) {
t = ce;
} catch (ExecutionException ee) {
t = ee.getCause();
} catch (InterruptedException ie) {
Thread.currentThread().interrupt(); // ignore/reset
}
}
if (t != null)
System.out.println(t);
}
}
However spock seems to stop running when it hits the Object result = ((Future<?>) r).get() line of code
Is this a know issue?
Something I am doing wrong?
The spock version is:
version='0.7-groovy-2.0'
Related
I want to ask something, yesterday I was using Dart version 2.10, after I upgraded to version 2.13, I can't run this code can someone help me
class AmtException implements Exception {
String errMsg() => 'Amount should be greater than zero';
}
void withdraw_amt(int amt) {
if (amt <= 0) {
throw new AmtException();
}
}
void main() {
try {
withdraw_amt(-1);
} catch (e) {
print(e.errMsg());
} finally {
print('Ending requested operation.....');
}
}
This output :
Error: The method 'errMsg' isn't defined for the class 'Object'.
- 'Object' is from 'dart:core'.
Try correcting the name to the name of an existing method, or defining a method named 'errMsg'.
print(e.errMsg());
^^^^^^
The final code:
class AmtException implements Exception {
String errMsg() => 'Amount should be greater than zero';
}
void withdraw_amt(int amt) {
if (amt <= 0) {
throw new AmtException();
}
}
void main() {
try {
withdraw_amt(-1);
} on AmtException catch (e) {
print(e.errMsg());
} finally {
print('Ending requested operation.....');
}
}
Thanks #jamesdlin
I am new to dart and I am learning dart from youtube. And courses that I am following are of 2018. The programs that they created in their videos are not working. I am facing the below issue in all my programs. Anyone, please guide me that why the programs show errors while the programs are running properly in their videos. Is it happening due to an update in dart? or any other reason? Please help to fix this issue. Thanks!
The function 'errorMessage' isn't defined.
Try importing the library that defines 'errorMessage', correcting the name to the name of an existing function, or defining a function named 'errorMessage'.
class CustomException implements Exception {
String errorMessage() {
return ("Invalid Amount");
}
}
void AmountException(int amount) {
if (amount <= 0) {
throw new CustomException();
}
}
void main() {
try {
AmountException(0);
} catch (e) {
print(errorMessage());
}
}
You are not calling the errorMessage() message on the exception. Another problem is that your catch is set to handle all types of exceptions. Since Exception does not have the errorMessage() method, you cannot call it.
You should therefore specify the type of exception you want to catch which will allow you to call the errorMessage() method on the catched exception:
class CustomException implements Exception {
String errorMessage() {
return ("Invalid Amount");
}
}
void AmountException(int amount) {
if (amount <= 0) {
throw new CustomException();
}
}
void main() {
try {
AmountException(0);
} on CustomException catch (e) {
print(e.errorMessage());
}
}
I have trouble getting data here from database there isn't data,
Data is not displayed outside of the method.
Could you please help me?
List<Person> Refresh() {
Person p = new Person();
ParseQuery <ParseObject> query = ParseQuery.getQuery(NAME_DATABASE);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> scoreList , ParseException e) {
if (e == null) {
for (int i = 0; i < scoreList.size(); i++){
p.setId(scoreList.get(i).getInt(Key.ID));
p.setName(scoreList.get(i).getString(Key.NAME));
p.setAge(scoreList.get(i).getString(Key.AGE)); p.setDate_start(scoreList.get(i).getString(Key.DATE_START));
p.setMonth_number(scoreList.get(i).getString(Key.MONTH_NUMBER));
p.setPropriety(scoreList.get(i).getString(Key.PROPRIETY));
p.setPrice(scoreList.get(i).getString(Key.PRICE));
p.setGender(scoreList.get(i).getString(Key.GENDER));
persons.add(p); //there is find data
}
}
else {
Log.d("score", "Error: " + e.getMessage());
}
}
});
return persons; //here there isn't data (size=0)
}
The answer is simple really. You are returning persons outside the findInBackground() method.
else{
Log.d("score", "Error: " + e.getMessage());
}
//Return here after the else statement
return persons;
}
Note : you might want to make persons a global variable or else android studio will tell you to declare it as a final variable.
I wrote my own custom error handler for the UI in Vaadin flow. But when I throw the exception my view crash and not show my human readable error message.
I did this in other application using Vaadin 8 and works perfectly. The idea its throw a SgiException in my backend services like:
Product not found
Incorrect value for field "XXX"
Not available stock for the product.
etc.
And then show a system notification
public static void setDefaultErrorHandler(ErrorEvent errorEvent) {
Throwable t = DefaultErrorHandler.findRelevantThrowable(errorEvent.getThrowable());
String message;
if (t != null) {
message = t.getMessage();
} else {
message = "";
}
log.error(message, t);
SgiException sgiException = getCauseOfType(t, SgiException.class);
if (sgiException != null) {
NotificationBuilder.exception(sgiException.getCode(), sgiException.getMessage());
return;
} else {
NotificationBuilder.exception(UNKNOW_ERROR, (message == null ? "" : message));
return;
}
}
private static <T extends Throwable> T getCauseOfType(Throwable th, Class<T> type) {
while (th != null) {
if (type.isAssignableFrom(th.getClass())) {
return (T) th;
} else {
th = th.getCause();
}
}
return null;
}
And this is how I set the custom error handler:
#PostConstruct
public void configBaseView() {
VaadinSession.getCurrent().setErrorHandler(Util::setDefaultErrorHandler);
}
In the view show this:
Note:
Debugging the application, seeing the code it's running, looks the method its called for some reason not show the notification.
This is a nasty behaviour that can't currently be overridden in Vaadin 10. Follow and vote (thumb up or comment) this issue to get it solved: https://github.com/vaadin/flow/issues/801
In Dart, how would I best code the equivalent of an (immutable/value/non-object) out or reference parameter?
For example in C#-ish I might code:
function void example()
{
int result = 0;
if (tryFindResult(anObject, ref result))
processResult(result);
else
processForNoResult();
}
function bool tryFindResult(Object obj, ref int result)
{
if (obj.Contains("what I'm looking for"))
{
result = aValue;
return true;
}
return false;
}
This is not possible in Dart. Support for struct value types, ref or val keywords were discussed on the Dart mailing list just like week. Here is a link to the discussion where you should let your desire be known:
https://groups.google.com/a/dartlang.org/d/topic/misc/iP5TiJMW1F8/discussion
The Dart-way would be:
void example() {
List result = tryFindResult(anObject);
if (result[0]) {
processResult(result[1]);
} else {
processForNoResult();
}
}
List tryFindResult(Object obj) {
if (obj.contains("What I'm looking for")) {
return [true, aValue];
}
return [false, null];
}
you can also use a tuple package like tuple-2.0.0
add tuple: ^2.0.0
to your pubspec.yaml
then any function can return many typed objects like this:
import 'package:tuple/tuple.dart';
Tuple3<int, String, bool?>? returnMany() {
return ok ? Tuple3(5, "OK", null) : null;
}
var str = returnMany().item2;
In your case:
void example() {
var result = tryFindResult(anObject);
if (result.item1) {
processResult(result.item2!);
} else {
processForNoResult();
}
}
Tuple2<bool, int?> tryFindResult(Object obj) {
if (obj.contains("What I'm looking for")) {
return Tuple2(true, aValue);
}
return Tuple2(false, null);
}
you can throw an exception too when no result.
void example() {
var result = tryFindResult(anObject);
try {
processResult(result);
} on NullException catch(e){
processForNoResult();
}
}
int tryFindResult(Object obj) { // throws NullException
if (obj.contains("What I'm looking for")) {
return aValue;
}
throw NullException();
}