Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 570
  • Last Modified:

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?
0
surajguptha
Asked:
surajguptha
  • 4
  • 4
  • 4
  • +5
2 Solutions
 
AndyAinscowCommented:
1) ? (Would there be exceptions thrown in such languages?)

2) Not really - the program crashes
0
 
wdosanjosCommented:
There are a few patterns. Here are the ones I'm familiar with:

. Functions return an 'error code', which has to be inspected by the caller for any errors.

. Functions are provided to manage the last error.  For example, the Win32 API has the SetLastError and GetLastError functions.  Functions before returning would SetLastError, and the caller would call GetLastError to inspect if the call succeeded.

Both approaches can be used in C#, and are in fact when handling directly with the Windows API.
0
 
AndyAinscowCommented:
@wdosanjos - Those are error handling, not exception handing aren't they?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
dimajCommented:
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
0
 
wdosanjosCommented:
@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.
0
 
wdosanjosCommented:
@AndyAinscow, but you are right. That's technically not Exception Handling, but Error Checking.
0
 
surajgupthaAuthor Commented:
@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)?
0
 
anarki_jimbelCommented:
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
0
 
wdosanjosCommented:
> 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.
0
 
Todd GerbertIT ConsultantCommented:
>> 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.
0
 
rd707Commented:
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
0
 
rd707Commented:
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.
0
 
AndyAinscowCommented:
I'd suggest a split
#35072715
#35072737
0
 
rd707Commented:
#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
0
 
AndyAinscowCommented:
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.
0
 
rd707Commented:
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...
0
 
DhaestCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 4
  • 4
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now