Unable to catch .NET errors in IronPython - error-handling

i could not error handling Error from .NET Dll when call different methods of an object. I got this error with IronPython:
Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at Microsoft.Scripting.Interpreter.FuncCallInstruction`2.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.LightLambda.Run3[T0,T1,T2,TRet](T0 arg0, T1 arg1, T2 arg2)
at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
I added try and exception as:
import System
for i in dir(object):
try:
getattr(object,i)()
expcept System.AccessViolationException as e:
print i,e
expcept Exception as e:
print i,e
However, the Unhandled Exception still crashes my code. How to make error handling in this case.

Related

Sleuth/zipkin with ControllerAdvice is not working

I found there is an old issue Sleuth/Zipkin tracing with #ControllerAdvice, but I meet the same problem with the latest version(spring-cloud-starter-zipkin:2.1.0.RELEASE), I debug it and find that the error is null, so zipkin just guess with statuscode. I have to throw the exception again to make zipkin notify the exception
error is null
zipkin result
ControllerAdvice
throw the exception again, it works
It makes perfect sense that it's null. That's because YOU control the way what happens with the caught exception. In your case, nothing, cause you swallow that exception.
If you want to do sth better, just add the error tag manually via the SpanCustomizer. That way you'll add the exception to the given span. It will then automatically get closed and reported to Zipkin (you can do sth else than ex.toString() of course.
#Slf4j
#RestControllerAdvice
#Order(Ordered.HIGHEST_PRECEDENCE)
public class ExceptionHanders {
private final SpanCustomizer customizer;
public ExceptionHanders(SpanCustomizer customizer) {
this.customizer = customizer;
}
#ExceptionHandler({RuntimeException.class})
#ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public String handleRuntimeException(Exception ex) throws Exception {
this.customizer.tag("error", ex.toString());
return "testabcd";
}
}

Why, when I am testing that a method throws an exception and the method throw an exception, does the test stop?

I have a unit test that tests if method throws an exception when condition is present, and method does throws exception as expected.
- (void)testMethodThrowsWhenConditionIsPresent {
XCTAssertThrows([Foo methodWithCondition: condition], #"Condition is true, method should throw exception");
}
Here is the exception source:
- (void)methodWithCondition:(someType)condition {
if (condition) {
[NSException raise: #"condition is true!" format: #"condition is true!"];
}
}
Why does the test stop at the line the exception is thrown? The test does not go on, it stops at that line, when I expect it to continue and return 1 from XCTAssertThrows(), making the test succeed. The test instead stops with Xcode bringing me to the line it was thrown, with a green `Thread 1: breakpoint 1.1' and the debugger appearing in the console.
Why does the test stop when the execution is thrown?
Because you have a breakpoint, which stops execution.
Why, after removing the breakpoint, does my application crash when the exception is thrown?
Because you have an unhandled exception. Unhandled exceptions cause your program to crash.
How can I handle an exception so it won't crash my program?
The easy answer to this question is to simply NOT throw an exception. In other programming languages, like Java, this is perfectly standard. But in Objective-C, we don't really do exceptions. In Objective-C, exceptions should be saved for TRULY exceptional behavior.
With that said, and a strong suggestion for you to find another way to handle whatever it is you're trying to handle, this is how you handle an exception in Objective-C:
#try {
// code that could throw an exception
}
#catch (NSException *e) {
// handle the exception...
}
#finally {
// post try-catch code, executed every time
}

SoapException Not Caught in try catch

Take the following try-catch statement in a WCF-Service
Try
'Here some method is being called.
Return myBs.PerformDailyUpdate()
Catch ex As Exception 'Exception is not being caught here
If service IsNot Nothing AndAlso service.HasWarnings Then
TextFileTracer.Write(String.Format("Warning in method: '{0}'.", name))
TextFileTracer.Write(service.GetWarnings)
End If
Try
TextFileTracer.Write("Error in dbmanager: " & service.HasErrors.ToString)
Catch ex2 As Exception
End Try
TextFileTracer.Write(String.Format("Error in method: '{0}'.", name))
TextFileTracer.Write(ex.Message & vbCrLf & ex.StackTrace)
End Try
End Function 'Exception shows here while debugging
In that method (PerformDailyUpdate) an ASMX-Webservice (.Net 2.0) is being called. This webservice throws occassionaly an SOAPException caused from a method being called from that webservice.yet somehow this SOAPException is nog being caught by the method above.
My Question: Why isn't the SOAPException being Caught? Are there some characteristics that seperates a SOAP-Exception from normal 'exceptions' generated (that in turn cause it not be caught)?
Note: The code written here is not mine. So Please don't judge me on it
ExceptionMessage (First part)
System.Web.Services.Protocols.SoapException: Unexpected application error: SqlException.
ADF.ExceptionHandling.GlobalExceptions.UnexpectedException: Unexpected application error: SqlException.
System.Data.SqlClient.SqlException: Incorrect syntax near ')'.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
....
Thanks
Scheme (how this clarifies the situation somewhat):
Internal DailyTriggerMechanism -----> WCF - Service -----> ASMX-Webservice ----> DB2/SQL
(Origin code above) (Exception being thrown)
Inside the WCF-Service the data recieved is being manipulated to fill specific tables. The AMSX-Webservice may not change in this situation.
I think a normal exception should be able to catch that unhandled error you're listed, but you might try a SOAP-specific, and/or SQL-related exception, in addition to your regular exception.
Like:
try {
//your failing code
}
catch (SOAPException se)
{
//your response }
catch (SQLException sqle)
{
//your response
}
catch (Exception e)
{
//your response
}

Handling exception in Javaparser

I am trying to handle the exception produced by Javaparser library due to token error. I used the following code.
String content=getTheSource();
ByteArrayInputStream bin=new ByteArrayInputStream(content.getBytes());
try
{
CompilationUnit cu=JavaParser.parse(bin);
} catch (Exception e) {
// TODO Auto-generated catch block
//e.printStackTrace();
//my handling code here
}finally{
bin.close();
}
However, the exception was never caught and I am getting a different exception generated from somewhere else. I got this exception:
Exception in thread "main" japa.parser.TokenMgrError: Lexical error at line 1, column 16. Encountered: "#" (35), after : ""
at japa.parser.ASTParserTokenManager.getNextToken(ASTParserTokenManager.java:2247)
at japa.parser.ASTParser.jj_ntk(ASTParser.java:9986)
at japa.parser.ASTParser.ClassOrInterfaceBody(ASTParser.java:926)
at japa.parser.ASTParser.ClassOrInterfaceDeclaration(ASTParser.java:604)
at japa.parser.ASTParser.TypeDeclaration(ASTParser.java:524)
at japa.parser.ASTParser.CompilationUnit(ASTParser.java:269)
at japa.parser.JavaParser.parse(JavaParser.java:81)
at japa.parser.JavaParser.parse(JavaParser.java:94)
at misc.CompileTest.main(CompileTest.java:45)
Any idea, how to handle the exception? Thanks in advance
As the name indicates, TokenMgrError is an error. So you have to catch an Error instead of Exception. If you want to catch both Error and Exception, you can use Throwable instead.
Originally, this error is throwed by JavaCC (TokenMgrError) which is used by Javaparser.
From version 3 on, JavaParser will/should not throw this error anymore.

Eclipse IIntroPart throwing exception when trying to load ActionBars

I have a class implementing IIntroPart and inside the creation method-
public void createPartControl(Composite container) {
Browser browser = new Browser(container,SWT.NONE);
browser.setUrl(STRHTMLPATH+File.separator+"wc.html");
IActionBars bars = getIntroSite().getActionBars(); //NULL POINTER
First two lines are working perfectly alright and its loading the browser as well, but when I am trying to load the actionBars as I do in View, I am getting NullPointerException here.
What is the issue.
Unable to create view ID org.eclipse.ui.internal.introview: An
unexpected exception was thrown.
Above is the message thrown before the Exception Stack