Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'd like make a general survey asking what members here think is best way(s) to Raise and Handle Errors and Exceptions.

Method 1: throw Exceptions
string method1()
{
if(notSuccessful)
throw new Exception("reason");

//or
try{

}
catch(Exception e)
{
	throw e;
}
}


Method 2: return null or empty object

MyObject method2()
{
MyObject obj = null;

if(success){
obj=new MyObject();
}

return obj;
}


Method 3: return bool or int and save object data
private myObject; //class variable

public GetMyObject(){return this.myObject;}

bool method3(){

this.myObject = null;

if(success){
this.myObject=new MyObject();
}

return success;
}


Method 4: HasError and GetLastError

public bool HasErrors
{
	get { return this.hasErrors; }
}

public string LastError
{
	get { return this.lastError; }
}


What do you think is best approach?
Posted

1 solution

All wrong, without any exclusions. That was the purpose of structural exception handling to avoid such situations.

Maybe, only the first case deserve some comment. This code is logically equivalent to not handling exceptions at all, but practically it is wasteful: it just spends some extra resources, including development and support of those superfluous lines, nothing else. The exception should be re-thrown only for certain purpose: to create selective handling at some point, by exception type or on some condition. In such cases, some of the conditional situation is handled locally, and some are re-thrown up the stack (do you understand stack operation? this is the key), to be handled elsewhere. Also, it is important not to handle exceptions locally. One of the most important purposes of the mechanism is to minimize handling and neatly separate "normal" execution flow from exceptional; therefore, in most places in the code, exceptions should just go, propagate further up the stack, which is achieved by not handling exceptions.

So, what to do? First you need to get some basic understanding on how exception works. This is a way to bypass normal call-return order based on call stack; exceptions system can be understood as some kind of "time machine". I explained it here:
Does Exception in C# Constructor Cause Caller Assignment to Fail?[^],
where was stored .net exceptions in operating system[^].

Please see my other past answers with some basic recommendations:
Handling exceptions in class library (dll)[^],
Exception Details: System.Runtime.InteropServices.COMException: Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error:...[^],
throw . .then ... rethrowing[^],
When i run an application an exception is caught how to handle this?[^],
How do i make a loop that will stop when a scrollbar reaches the bottom[^],
Keep a c# windows form application running despite any unhandled exception[^],
Unhandled Exception : Access Violation[^],
Error Logging and Screen Shot.[^],
Catching an Exception[^].

But you should understand that real refined strategy in each cases should come from deep understanding of the mechanism and appropriate experience with code design, development and maintenance.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900