Solved

Exception Handling without Try/ Catch Blocks

Posted on 2011-03-08
19
550 Views
Last Modified: 2012-06-27
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
Comment
Question by:surajguptha
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 4
  • +5
19 Comments
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 250 total points
ID: 35072715
1) ? (Would there be exceptions thrown in such languages?)

2) Not really - the program crashes
0
 
LVL 23

Assisted Solution

by:wdosanjos
wdosanjos earned 250 total points
ID: 35072737
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
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 35072765
@wdosanjos - Those are error handling, not exception handing aren't they?
0
Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

 
LVL 7

Expert Comment

by:dimaj
ID: 35072776
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
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35072847
@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
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35072882
@AndyAinscow, but you are right. That's technically not Exception Handling, but Error Checking.
0
 
LVL 21

Author Comment

by:surajguptha
ID: 35072895
@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
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 35073157
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
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35073180
> 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
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35073247
>> 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
 
LVL 4

Expert Comment

by:rd707
ID: 35074917
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
 
LVL 4

Expert Comment

by:rd707
ID: 35277403
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
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 35279444
I'd suggest a split
#35072715
#35072737
0
 
LVL 4

Expert Comment

by:rd707
ID: 35279701
#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
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 35279843
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
 
LVL 4

Expert Comment

by:rd707
ID: 35280188
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
 
LVL 53

Expert Comment

by:Dhaest
ID: 35718295
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Progress

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question