Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 15th, 2005, 2:24 PM   #1
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
forcing exit in a console program

Hi
I'm writing a console program and at a point (after an exception occurs) i want the program to stop executing and to not call the rest of the functions since the constructor is doing the calls.

Note: i can't use Application.Exit() since i am not using System.Windows.Forms

private void ReadAndSplit()
{
	string text_line, file_name = @"input.txt";
	StreamReader file_reader = null;
	try
	{
		file_reader = File.OpenText(file_name);
		
		//read and split the line into words and add it to the array
		while((text_line = file_reader.ReadLine()) != null)
			full_words.AddRange(text_line.Split());	
	}
	catch(IOException exp)
	{
		Console.WriteLine(exp.Message);
	}
	finally
	{
		if(file_reader != null)
			file_reader.Close();
	//I want it to exit HERE
	}
}

Any ideas?
OpenLoop is offline   Reply With Quote
Old Jun 15th, 2005, 2:45 PM   #2
Dizzutch
Professional Programmer
 
Dizzutch's Avatar
 
Join Date: Dec 2004
Location: Worcester, MA
Posts: 441
Rep Power: 4 Dizzutch is on a distinguished road
Send a message via ICQ to Dizzutch Send a message via AIM to Dizzutch Send a message via MSN to Dizzutch Send a message via Yahoo to Dizzutch
Java has something called System.exit(int code) which exits with an error code. So does C/C++ (exit(int)). I'm sure C# has it as well. I'm just not familiar with it, so i don't know the function name.
-Dizz
__________________
naked pictures of you | PFO F@H stats
Dizzutch is offline   Reply With Quote
Old Jun 15th, 2005, 4:34 PM   #3
Arla
Professional Programmer
 
Arla's Avatar
 
Join Date: Mar 2005
Posts: 332
Rep Power: 4 Arla is on a distinguished road
Couldn't you throw the exception back to the constructor and then use the constructor to catch the exception (and stop execution).
Arla is offline   Reply With Quote
Old Jun 15th, 2005, 4:34 PM   #4
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Use a goto statement (Don't know if C# has that, C and C++ do )

EDIT: Okay yup it has it, see here.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates

Last edited by nnxion; Jun 15th, 2005 at 4:40 PM.
nnxion is offline   Reply With Quote
Old Jun 15th, 2005, 4:59 PM   #5
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Don't use goto. Make the function that's throwing an exception return a boolean - return true if it executes successfully, and false if it doesn't. Then, in your main function, you can check the return value and prematurely return 1; if you wish.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Jun 15th, 2005, 5:09 PM   #6
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Why not use a goto statement? It's the best
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Jun 15th, 2005, 6:41 PM   #7
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
I put exception handling on all my functions so that all exceptions will be caught until the program terminates.

Ooble I like you approach, but Arla's idea is more pro.

nnxion, using a goto statement is a bad practice in C languages, besides if you use a goto statement you must make sure that it won't jump over a member definition otherwise you'll get a compiler error

Thanks
OpenLoop is offline   Reply With Quote
Old Jun 15th, 2005, 11:55 PM   #8
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 406
Rep Power: 5 xavier is on a distinguished road
Send a message via Yahoo to xavier
I also found on google -Environment.Exit()- .
Here's a link that talks about Application.Exit() and Environment.Exit().
http://geekswithblogs.net/mtreadwell...6/06/6123.aspx
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Old Jun 16th, 2005, 8:04 AM   #9
Dizzutch
Professional Programmer
 
Dizzutch's Avatar
 
Join Date: Dec 2004
Location: Worcester, MA
Posts: 441
Rep Power: 4 Dizzutch is on a distinguished road
Send a message via ICQ to Dizzutch Send a message via AIM to Dizzutch Send a message via MSN to Dizzutch Send a message via Yahoo to Dizzutch
Quote:
Originally Posted by nnxion
Use a goto statement (Don't know if C# has that, C and C++ do )

EDIT: Okay yup it has it, see here.
I might have to see if i can have you banned from ever compiling any source code again, since you just recommended a GOTO statement. ..
j/k
in all fairness, using GOTO is a horrible programming practice, it gets rid off all the direction in your code, and makes a horrible mess out of everything. I'm sure y'all know that, but i have a tendancy of explaining stuff to rookies.
__________________
naked pictures of you | PFO F@H stats
Dizzutch is offline   Reply With Quote
Old Jun 16th, 2005, 12:19 PM   #10
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Yeah I know why a goto is bad in most programming cases but if you just want to plain ugly exit then you might as well use it.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:21 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC