![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
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? |
|
|
|
|
|
#2 |
|
Professional Programmer
|
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 |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Mar 2005
Posts: 332
Rep Power: 4
![]() |
Couldn't you throw the exception back to the constructor and then use the constructor to catch the exception (and stop execution).
|
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
__________________
"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. |
|
|
|
|
|
#5 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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.
|
|
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#7 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#8 |
|
Professional Programmer
|
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 ! |
|
|
|
|
|
#9 | |
|
Professional Programmer
|
Quote:
![]() 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. |
|
|
|
|
|
|
#10 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|