Try Catch statement realBasic - basic

I am trying to wrap up simple statement within try catch. Problem is that all examples that I was able to find talk about the errors which are predefined.
I need a generic error, similar to try/catch in C#.

For a generic try catch you can do this:
try
...put some code here
catch
...do something for ANY exception here.
finally
...code here that runs IF an exception occurs
end try
The catch section has optional parameters to catch certain types of errors (the examples you've seen). The first paragraph gives the definition at http://docs.xojo.com/index.php/Try
Try
// Your code
Catch [ErrorParameter] [As ErrorType]
//exception handlers
[ Finally ]
//code that executes even if runtime exceptions were raised
End [Try]

Related

unrecognised exception in windows C++ code

We have a number of sections of code in the format:
try
{
// code
}
catch(std::exception &e)
{
// log exception
}
catch(...)
{
// log unknown exception.
}
Every so often, the unknown exception code triggers, and logs an unknown exception.
I always thought that all exceptions were meant to derive from std::exception, and thus catching std::exception would catch all exceptions.
Is there some other exception that I should be catching?
If my code ends up in the unknown exception handler, is there any way that I can find out what exception was actually caught?
edit
We managed to locate the cause of the problem- despite saying that they had, the customer had not installed .NET 3.5, which our code depends on, and the system fell over when trying to use the XML parser.
Is there some other exception that I should be catching?
This depends on your code. Libraries you call can throw exceptions not derived from std::exception, examples are MFC's CException or Microsoft's _com_error. Also, an access violation might be catched by catch(...), which is the reason why I would not use catch(...) in my code - it's just to broad for me.
2.If my code ends up in the unknown exception handler, is there any way that I can find out what exception was actually caught?
You can run your code in the debugger and configure the debugger to break your program when the exception is thrown (first chance). Then you know exactly which line of code triggers the exception and should be able to see what exactly is thrown.

Should async function never ever throw?

I wrote a library with a number of async functions.
A SYNCHRONOUS helper function throws an error if one of the parameters is plain wrong:
proto.makeParameters= function( filters ){
default:
throw( new Error("Field type unknown: " + fieldObject.type ) );
break;
}
In my async functions, when I use it, I have:
proto.someAsyncFunction = function( cb ){
// Run the query
try {
var parameters = this.makeParameters( filters );
} catch( e ){
return cb( e );
}
}
So:
Is it good practice that asyncfunctions should never ever throw? (Like I did)
Right now, I am catching ALL errors. Shall I e more choosy? Maybe make up an error type and just check for that? If so, what should I do in either case?
Your assumptions on the async code are correct. See this post by Isaac Schlueter himself on the topic:
The pattern in node is that sync methods throw, and async methods pass
the error as the first argument to the callback. If the first
argument to your callback is falsey (usually null or undefined), then
all is well with the world.
http://groups.google.com/forum/#!msg/nodejs/W9UVJCKcJ7Q/rzseRbourCUJ
Is it good practice that async functions should never ever throw? (Like I did)
async functions, of course, will throw exceptions whenever we like it not, simply because of the software imperfection. So throwing custom exception is completely fine but what is important is how to correctly catch them.
The problem is that differently from sync code the stack of the async exception may not be available. So when the exception occurs, it is not always possible to say where to return the control and where is the handler. node.js has two methods of specifying what to do when the exception in asynchronous code occurs: process uncaughtException and domains.
As you see, dealing of the exceptions in async code is tricky so throwing an exception should be considered as a last option. If the function just returns status of the operation it is not an exception.
It seems for me that in the provided code fragment the exception is thrown correctly because it indicates that the method was called improperly and cannot do its job. In other words, the error is permanent. This indicates serious flaw in the application that should be fixed. But if the function cannot create a parameter for some temporary reason that can be fixed without modifying the application, then returning the status is more appropriate choice.

c# Throw new Exception with in Loop OK?

I've a list of records retrieved through external web service. Some of the data are rubbish and would like to log the record that failed by throwing a new exception.
Wondering if this is best way to handle, as i read exception can impact the performance?
Pseudo code e.g.
try
Loop through listOfRecords
perform logic.
catch
throw new exception (record details)
It depends on what you want to do with the rest of the records when an exception is encountered. Your current implementation would terminate the loop on the first error.
If, however, you simply want to mark the record in some way as "unprocessed" or "errored" and continue with the remaining records, you'd want to handle the error entirely within the loop:
foreach (var record in records)
{
try
{
Process(record);
}
catch (TypedException ex)
{
LogError(ex);
MarkAsUnprocessed(record, ex);
// respond in some other way as well?
}
}
Ultimately, the exception handling logic belongs wherever it logically makes sense to handle the exceptions. Catching exceptions is the easy part, that's a simple construct of the language being used. Meaningfully handling exceptions is another story entirely, and is a logical construct independent of the language being used.
Edit: It's also very much worth noting that your pseudo-code implies this way of handling exceptions:
try
{
// something
}
catch
{
throw new Exception("something");
}
This is very bad practice. Throwing an entirely new exception essentially tosses out the exception that was caught, which gets rid of some very useful debugging information (not the least of which being the stack trace). If you want to immediately log the exception and then let it continue up the stack, simply use the keyword throw by itself:
try
{
// something
}
catch (Exception ex)
{
// log the exception
throw;
}
If you want to add context to the exception, you can modify its properties before throwing it again. Or perhaps even better, throw a custom exception (not Exception) and attach the existing one as the InnerException property:
try
{
// something
}
catch (Exception ex)
{
throw new RecordProcessingException("some additional context", ex);
}
If, on the other hand, there's nothing meaningful to be done with the exception in the current context, don't even catch it at all. Let it bubble up the stack until it encounters code that can meaningfully handle it.
It's not the best way, since you only need the log-writing functionality. Just write to the log and continue the loop.

How to let program to run instead of an exception in c#?

I am writing a program to read all files from an array.
Suppose in between if any file is corrupt or any reason it will stop the execution in between and throw an exception
I want to let the code running till end and at last it logged the error files instead of throwing exception?
try{
doSomethingThatMayRaiseAndException();
}
catch (Exception e){
NotifyTheUserThatSomethingBadJustHappened();
}
Exception here is the base class for exceptions, you may need to use a more specific one if you want to provide the user with details. But right now, what you need to learn is how to deal with exceptions. You can use the link provided by Oded, it is a good start. Then note what is the raised exception you need to handle, and handle it.

J2ME NullPointerException not getting caught

try{
imgball = Image.createImage("/ball.jpg");
//imgpad = Image.createImage("/ball.jpg");
}
catch(Exception e)
{}
The above code works as it is. But when i open imgpad statement, it gives me error of uncaught NullPointerException ? What can be wrong ?
P.S. I am working in a different Thread. If that matters.
The NullPointerException (NPE) must be later in your code. Your catch block will catch any NPE during image load.
As mdma mentioned, the NullPointerException must be later because when the Image.createImage("/ball.jpg"); fails, it will throw an Exception that you catch. Since you catch it and then do nothing, the value of imgball will be unset (null).
Since you are working from a different Thread, it's possible that you are accessing the variable too soon, but I assume the above reason is more accurate because imgball will probably always fail to be created since you give it the absolute path.
Ok. It was my mistake. I'd like to make it clear here for others to know.
The mistake I made was actually from the main thread. I'd written following :
refcan = new ReflectCanvas(2);
d.setCurrent(refcan);
And I was loading images in the constructor ReflectCanvas(). So it could bear the speed upto one image but not for two :)

Resources