Link to home
Start Free TrialLog in
Avatar of surajguptha
surajgupthaFlag for United States of America

asked on

Exception Handling without Try/ Catch Blocks

1) What are the known patterns available for handling exceptions in programming languages that doesnt have a try/ catch block?

2) Is there a way to handle exceptions without try / catch block in c# .net?
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@wdosanjos - Those are error handling, not exception handing aren't they?
You could handle exceptions in C# w/out try catch by handling the UnhandledException event.

http://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx
@AndyAinscow that's how languages that don't have exception handling handle error conditions.  

For example, in C the fopen stdio function returns NULL when it cannot perform the operation. The calling program then needs to check the errno for the reason.
@AndyAinscow, but you are right. That's technically not Exception Handling, but Error Checking.
Avatar of surajguptha

ASKER

@Andy : It makes sense that such programming languages will not throw exceptions. But how can such cases be handled in such programming languages? Does the program crash too?

@wdosanjos: How would such a function return an error code if an index array out of bounds case were to occur within itself(without using try/ catch)?
I don't want to argue but many people use "error handling" and "exception handling" interchangeably.

A huge application I'm working on contains two approaches: try/catch and old "on error go to". The application was written in VB4/5/6 and converted to .Net. In some sense both mechanisms are similar - they handle errors that happen in a system.

Interesting article on handling errors/exceptions, including unhandled ones (say, in ASP):

http://dotnetguts.blogspot.com/2007/10/error-handling-in-net-with-example.html
> How would such a function return an error code if an index array out of bounds case were to occur within itself(without using try/ catch)?

I guess it depends on the language.  In C for example, the program will return a value even if the index array is out of bounds.  The C program would only crash if it's trying to access protected memory.  Basic has the ON ERROR statement that determines what to do in case of errors, it can go to specific line of code, ignore the error, or set the Err.Number variable.
>> How would such a function return an error code if an index array out of bounds case were to occur within itself(without using try/ catch)?

I'm really only familiar with .Net, but my understanding is that in all such "high-level" languages, juch as Java and even C++ .Net, exceptions are handled with try/catch.  Other languages, so-called "low-level" languages, like C and C++, generally require you (the programmer) to know what you're doing.

In fact, in C it is perfectly valid to write:
int main(int argc, char* argv)
{
	// Declare int array with 10 elements
	int intarray[10];

	// Set the value of an element well beyond the upper-limit of the array
	intarray[65534555] = 13;
}

Open in new window


In this case the compiler will not complain, and your program may or may not run.  Really you're accessing memory that hasn't been allocated to you yet - so if something else happens to be using that memory Windows may decide to kill your application; I suppose it's possible to cause a blue screen of death; your program might run without problem; you set the value of a particular byte of memory, and something else changes it without your knowledge causing gremlins in your program.
1) The various error trapping methods I've seen are:

a) Function return calls. Typically in C, 0 means success, non-zero means failure.
b) On error goto - as used in VB6 and earlier
c) RaiseException - some database languages and 4GLs use this to raise an error in the calling program
d) Custom error values - if say, a function is expected to return a positive value, you could return -1 or some other custom value to indicate an error

2) If you know what sort of exceptions a .NET call can raise and code very defensively, you can trap errors before the call is made - e.g. check a file exists before reading it, check a file is not read-only before writing to it etc etc
As an aside, exceptions aren't strictly speaking a language neutral concept - they're only really used in their pure form in OO (object orientated) languages.

Other languages may have system defined error numbers, abend codes (mainframe speak) or some other handling construct.
I'd suggest a split
#35072715
#35072737
#35072715

Sorry, but I'd disagree here -

1) It depends on your definition of "exception" - pure OO parlance or simple error handling

2) Potentially, yes there is - but again, see my comment above
1) Yes it depends on 'exception' hence I suggested a split.
2) A crash is still a crash, even if you can show some custom error message just before the program aborts.
2) Sorry, I was thinking of defensive coding rather than letting an OO exception through unhandled but again, we're back to the understanding of exception...
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.