i want to print error log and skip the invalid json when it parse wrong in flink sql. i know there is configuration like :
'json.ignore-parse-errors' = 'true'
but it doesnt match what i want. i can use try catch method when i parse json in java like this
try {
return GSON.fromJson(json, element.class);
} catch (Exception e) {
LOG.warn("parse error, json: {}, error: {}", json, e);
return null;
}
but i really dont know how to do it in flink sql.
is there any way to do this?
thanks all.
Related
I am writing if statement like that:
String value;
if(int.parse(value)) // I want to write a condition that if error appear while parsing
// (like value contains some strings) then if statement run
It can be achieved by writing try/catch
try{
int.parse(value)
}
catch (e) {
// implement if statement
}
But I want to do this with if statement
Well you can try a different approach using tryParse() method and check whether the result is null then the parse failed.
if (int.tryParse(value)==null){ // execute failed parse code }
Given the shortcode example below:
...
print("1 parsing stuff");
List<dynamic> subjectjson;
try {
subjectjson = json.decode(response.body);
} on Exception catch (_) {
print("throwing new error");
throw Exception("Error on server");
}
print("2 parsing stuff");
...
I would expect the catch block to execute whenever the decoding fails. However, when a bad response returns, the terminal displays the exception and neither the catch nor the continuation code fires...
flutter: 1 parsing stuff
[VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: type
'_InternalLinkedHashMap<String, dynamic>' is not a subtype of type
'List<dynamic>'
What am I missing here?
Functions can throw anything, even things that aren't an Exception:
void foo() {
throw 42;
}
But the on Exception clause means that you are specifically catching only subclass of Exception.
As such, in the following code:
try {
throw 42;
} on Exception catch (_) {
print('never reached');
}
the on Exception will never be reached.
It is not a syntax error to have on Exception catch as someone else answered. However you need to be aware that the catch will not be triggered unless the error being thrown is of type Exception.
If you want to find out the exact type of the error you are getting, remove on Exception so that all errors are caught, put a breakpoint within the catch and check the type of the error. You can also use code similar to the following, if you want to do something for Exceptions, and something else for errors of all other types:
try {
...
} on Exception catch (exception) {
... // only executed if error is of type Exception
} catch (error) {
... // executed for errors of all types other than Exception
}
Use:
try {
...
} on Exception catch (exception) {
... // only executed if error is of type Exception
} catch (error) {
... // executed for errors of all types other than Exception
}
The rule is that exception handling should be from detailed exceptions to general exceptions in order to make the operation to fall in the proper catch block and give you more information about the error, like catch blocks in the following method:
Future<int> updateUserById(int userIdForUpdate, String newName) async {
final db = await database;
try {
int code = await db.update('tbl_user', {'name': newName},
whereArgs: [userIdForUpdate], where: "id = ?");
return code;
}
on DatabaseException catch(de) {
print(de);
return 2;
}
on FormatException catch(fe) {
print(fe);
return 2;
}
on Exception catch(e) {
print(e);
return 2;
}
}
print("1 parsing stuff");
List<dynamic> subjectjson;
try {
subjectjson = json.decode(response.body);
} catch (_) { . // <-- removing the on Exception clause
print("throwing new error");
throw Exception("Error on server");
}
print("2 parsing stuff");
...
This works, but what is the rationale behind this? Isn't the type inconsistency an Exception?
As everybody or most of the people said, try to know exactly what error you are getting:
try{
}
catch(err){
print(err.runTimeType);
}
runTimeType will give you the type of data or exception you are getting or to put simple the exception itself.
And then do whatever you want. (Like if you are not getting the exception of what you expected, then try to fix that issue or change the exception.)
Another option is to go with general form.
Using the simple catch which will prompt every time.
The other possible reason for the catch bloc not to fire, as pointed out in this question, is missing brackets when throwing an exception.
Must write throw FormatException() instead of throw FormatException.
I had a issue with try catch but my problem was the API that I send http request, doesn't response of my request so that is why my request doesn't response anything and try catch didn't catch the error. So I suggest you to add timeout to your request so that if your api doesn't response your request after a while you can cancel your request with timeout. Here is an example how to use it;
try {
final response = await http.post(Url).timeout(Duration(seconds: 5));
} catch (error) {
print(error)
}
I have some spark code, I need to catch all exception and store to file for some reason, so I tried to catch the exception and print it but its print empty
try {
/* Some spark code */
}
catch {
case e: Exception => {
println(" ************** " + e.printStackTrace())
}
}
output currently printing nothing ************** ()
printStackTrace doesn't return a stacktrace. It just prints it into the stderr. If you want to store it in the file you can
a) call e.getStackTrace and save each element manually
b) call e.printStackTrace(s) where s is a PrintStream or a PrintWriter pointing to your output file.
[Please find below the link that has answer to query][1]
Scala: Silently catch all exceptions
I'm in a situation where I have a function executing an SQL query whose results I would like to pass into a function that will save them as a CSV file. I wrote this try-with-resource block to take advantage of AutoClosable:
public static ResultSet getRiderHistory(File file, Calendar date) throws FileNotFoundException {
try(Connection conn = new dbConnect("psql", "localhost", ****, "*****", "****", "").get_conn();
PreparedStatement pstmt = createPreparedStatement(conn, getSerialFromFile(file), date);
ResultSet rs = pstmt.executeQuery()) {
if(!rs.isBeforeFirst()) {
throw new SQLException("ResultSet retuned empty!");
}
return rs;
} catch (FileNotFoundException e) {
throw new FileNotFoundException();
} catch (Exception e) {
e.printStackTrace();
}
}
but the compiler is quitting with this error:
compile:
[javac] Compiling 2 source files to /home/****
[javac] /home/****/****.java:50: error: missing return statement
[javac] }
[javac] ^
[javac] 1 error
I think I understand the reason the nature of the problem. I'm guessing it's because I'm trying to return a resource that's going to be closed, instead of a copy of the results like I want (please correct me if I'm wrong). As I'm new to Java, however, I don't really know how to resolve this and get the results I want. Should I use a finally on the ResultSet somehow, or ought I change the return value into something like an ArrayList, an object that doesn't have to closed?
No, the error is saying that your last catch (Exception) block does not return a value. The body of the try returns a value, and the first catch throws an exception, but the last catch does nothing except print a stacktrace. All return paths in this method must either 1) return a ResultSet or null, or 2) throw an exception.
Suppose "doc" is some document I want to insert into a MongoDB collection and "collection" is the collection I am inserting the document into.
I have something like the following:
try
{
WriteConcern wc = new WriteConcern();
wc.W = 1;
wc.Journal = true;
WriteConcernResult wcResult = collection.Insert(doc, wc);
if (!string.IsNullOrWhiteSpace(wcResult.ErrorMessage) || !wcResult.Ok)
{
return ErrorHandler(...);
}
else
{
return SuccessFunction(...);
}
}
catch (Exception e)
{
return e.Message;
}
Basically, if the insertion fails for any reason (other than hardware no longer working properly) I want to handle it (through the ErrorHandler function or the catch clause), while if it succeeds I want to call SuccessFunction.
My question: Is the above code sufficient for error checking purposes? In other words, will all failed insertions be caught, so that SuccessFunction is never called in those situations?
You don't even need to do any checking. collection.Insert will throw an exception if the write was not successful when you are using any write concern other than unacknowledged.
If you want to know if an error occured, you need to catch a WriteConcernException.