![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jul 2004
Posts: 29
Rep Power: 0
![]() |
Hey guys, i got this program im making. It encrypts and decrypts files. The problem is that whenever an error occurs while encrypting or decrypting ( having put a read-only file, a running program, input wrong keys, enter wrong encryption type...) the program tells me that an error occured. Thats nice and everything but i have to close the program, restart it and then try again. I can't just correct the mistake. If i do correct the mistake and try again, i get an error message again.
Basically i want to be able to correct mistakes without having to restart program. Does anyone know what to do? It'll be great if someone can. Thanks in advance!
__________________
.::SharPCreativitY.CoM::. Come and visit the progress now! Crypto Project Needs Your Help! Click HERE for more info! - Version 0.2 | 95% Complete | <-- Getting more & more stable as the days go bye! - Version 0.3 | 50% Complete | <-- Help is on its way! - Version 0.5 | 25% Complete | <-- Really coming along nice ;) (will not work with win2000 & lower :() No i am not Crazy... I have my reasons for doing this. LOL |
|
|
|
|
|
#2 |
|
Programmer
|
try{}, catch{}, finally{}
Crypter:
Sounds like you need to use exception handling, if you are not already using it. This way, when your program encounters a fatal error, it will not shut down, it will simple be "caught" by your exception handler and it will excecute the code that you write in there. I would consider creating your own exception classes for each kinf of error that you may encounter, like : ReadOnlyFileException, ExeException, InvalidInputException, InvalidEncryptionTypeException, etc as well as using some built in ones like FileNotFoundException and just plain ol' Exception. I hope that helps...sorry I don't have any code samples right now... Sincerely, Kevin Parkinson |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Jul 2004
Posts: 29
Rep Power: 0
![]() |
Hey man considering you are the only person that replied to my msg i am really greatful for ne help you provide me.
Once again thank you, i will look into the matter further.
__________________
.::SharPCreativitY.CoM::. Come and visit the progress now! Crypto Project Needs Your Help! Click HERE for more info! - Version 0.2 | 95% Complete | <-- Getting more & more stable as the days go bye! - Version 0.3 | 50% Complete | <-- Help is on its way! - Version 0.5 | 25% Complete | <-- Really coming along nice ;) (will not work with win2000 & lower :() No i am not Crazy... I have my reasons for doing this. LOL |
|
|
|
|
|
#4 |
|
Programmer
|
Crypter:
No problem...I am sure you will find Exception handling to be a joy! here's an example of how you MAY use it: try
{
SqlConnection con = new SqlConnection( someconnectionstring );
con.Open()
}
catch( SqlException sqle )
{
//don't allow the program to crash because your database could
// not open
Console.WriteLine( sqle.toString() );
}
finally
{
if( con != null )
{
con.Close();
con.Dispose();
}
}
__________________
public class MySignature extends Post implements JavaSyntax throws NuttinHoney{
MySignature()
{
Salutation();
Name();
}
public string Salutation()
{
Screen.printLn( "Sincerely,\n" );
}
public string Name()
{
Screen.printLn( "Kevin Parkinson" );
}
}Last edited by new User(Kevin.Parkinson); Mar 25th, 2005 at 10:46 AM. |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Jul 2004
Posts: 29
Rep Power: 0
![]() |
i see how this works now, hmmm the "finally" is very important, i will try to implement something similar and will tell you the results. unfortunatly i am very tired atm and it might take till 2morow morning, its now 1AM here in Quebec.
Alright thanks very much though! PS: One thing i dont understand is what the "Console.WriteLine( sqle.toString() );" does n e who. Thakns again! Talk to you soon
__________________
.::SharPCreativitY.CoM::. Come and visit the progress now! Crypto Project Needs Your Help! Click HERE for more info! - Version 0.2 | 95% Complete | <-- Getting more & more stable as the days go bye! - Version 0.3 | 50% Complete | <-- Help is on its way! - Version 0.5 | 25% Complete | <-- Really coming along nice ;) (will not work with win2000 & lower :() No i am not Crazy... I have my reasons for doing this. LOL |
|
|
|
|
|
#6 | |
|
Programmer
|
Catching Exceptions
Quote:
Let's say for some reason the database connection could not open, then what would happen is the program would throw an Exception, more specifically a SqlException( a type of Exception that handles erros from SQL Server thjat inherits from the Exception class ). the part where it says "catch( SqlException sqle )" will be the code that can stop that Exception from ending the program. sqle, is simply the instane name I gave to the SqlException, and to make the example simple, I assumed a console application where you could tell the user about the error that just happened. Console.WriteLine() writes text to the command prompt, and sqle.ToString() will be deatils about why the error occured. So, Console.WriteLine( sqle.ToStiring( ) ); will announce the database error to the user at the command prompt, assuming the program is a console application!
__________________
public class MySignature extends Post implements JavaSyntax throws NuttinHoney{
MySignature()
{
Salutation();
Name();
}
public string Salutation()
{
Screen.printLn( "Sincerely,\n" );
}
public string Name()
{
Screen.printLn( "Kevin Parkinson" );
}
} |
|
|
|
|
|
|
#7 |
|
Newbie
Join Date: Jul 2004
Posts: 29
Rep Power: 0
![]() |
i see now that brilliant but how do i find the right exception to write after my
catch ( ...exception) {} should i be able to find these things on the net? cuz i am making this program but i am still working on how to get everything together, like these errors. Alright thanks for the explanation though! Take care
__________________
.::SharPCreativitY.CoM::. Come and visit the progress now! Crypto Project Needs Your Help! Click HERE for more info! - Version 0.2 | 95% Complete | <-- Getting more & more stable as the days go bye! - Version 0.3 | 50% Complete | <-- Help is on its way! - Version 0.5 | 25% Complete | <-- Really coming along nice ;) (will not work with win2000 & lower :() No i am not Crazy... I have my reasons for doing this. LOL |
|
|
|
|
|
#8 |
|
Programmer
|
Using Exceptions
Hey Crypter:
I find it brilliant too. I will answer the question about knowing which exception to use the best I can. Fortunately, the .NET Framework has a whole list of Exception classes that you can use in your application( eg: the previously mentioned SqlException, FileNotFoundException, etc ) which all inherit from the base class Exception. Note that, for example if you are trying to open a file and it's not there, .NET will throw an object called FileNotFoundException, which you can simply deal with by doing something like this in a catch clause( always comes after a try clause ): catch( FileNotFoundException instanceName )
{
string errorMessage ="Sorry, the file was not found " + instancename.ToString();
}...now, most of the time, you'll probably want to use one of .NET's built in Exception classes, but as you described before, you may need to describe your errors in a personal way( ie: the file type the user is trying to encrypt is not allowed, etc ). In this respect, you can create a user defined Exception class that inherits from Exception: public class MyException : Exception ...unfortunately, I have not tried this myself, so I can't suggest what to code in there, but certainly I would check it on Google. I will look for some information too, as it would be good for me as well. I hope that helps.You said that you are in Quebec...I am "not far" from you in Edmonton!
__________________
public class MySignature extends Post implements JavaSyntax throws NuttinHoney{
MySignature()
{
Salutation();
Name();
}
public string Salutation()
{
Screen.printLn( "Sincerely,\n" );
}
public string Name()
{
Screen.printLn( "Kevin Parkinson" );
}
} |
|
|
|
|
|
#9 |
|
Newbie
Join Date: Jul 2004
Posts: 29
Rep Power: 0
![]() |
hehe thats pretty kool! And thanks to you i looked up many types of exceptions! and implemented many of them. Since i am using forms i decided to display the message through a message box. I implemented these exceptions: UnauthorizedAccessException ,
CryptographicException, IOException, NullReferenceException. All these exceptions are caught in catch clauses and found after the try clause. Now that i have an idea of whats going on i realised what errors are causing my to close program to retry decryption. When i decrypt a file using the wrong password or encryption type, i get the "CryptographicException",( i have put a label to show different message for every exception besides the msg box) now when i replace the password or encryption type with the correct one using the same open file, it gives me the IOException saying that the file is already being used by another process. Would there be a way to use Flush() or Dispose() in a catch clause?or Finally clause? Because atm i have to Exit my application and reopen it to try to decrypt the file again. What i find interesting is that in he try clause i have these fsOut.Close(); cs.Close(); fsCrypt.Close();. But when i try putting these in a catch clause with Dispose or Flush instead of Close, it gives me an error while compiling saying that a reference is missing. Anyways this is a long message hope, you have more knowledge to share with me. And thanks again for the great replys! Take care! Crypter
__________________
.::SharPCreativitY.CoM::. Come and visit the progress now! Crypto Project Needs Your Help! Click HERE for more info! - Version 0.2 | 95% Complete | <-- Getting more & more stable as the days go bye! - Version 0.3 | 50% Complete | <-- Help is on its way! - Version 0.5 | 25% Complete | <-- Really coming along nice ;) (will not work with win2000 & lower :() No i am not Crazy... I have my reasons for doing this. LOL |
|
|
|
|
|
#10 |
|
Programmer
|
Crypter:
I am not sure, because I don't see all the code for you program, but what I am assuming is happening is that you need to implement the finallyclause. This may be why you have an existing instance of you file open when you go to open it again. For example, say you go to open a file, and the program throws an Exception, you do something with it, but you don't explicitly close the file, then the next time you try to open or read the file, it will give the error that it's already open. Correct me if I am not understanding what you've written, but I think something like this will solve your problem: public SomeFileIO()
{
//you may need to declare your file objects here so they will be
//accessible within the finally clause
try
{
//open a file
//read the file
//do some precious file processing
}
catch( SomeFileException e )
{
MsgBox.Show( e.message );
}
finally
{
if( theFileIsStillOpen() )
{
CloseTheFile();
//perhaps dispose of the file
}
}
}...note that if th try excecutes and there is an Exception thrown, then the order of processing will be: try->catch->finally, else it will be try->finally. This should help to not leave any files open after an Exception is thrown or not. I hope that helps. It would be great if you send me, or post some of your code, as what you are doing sounds interesting.
__________________
public class MySignature extends Post implements JavaSyntax throws NuttinHoney{
MySignature()
{
Salutation();
Name();
}
public string Salutation()
{
Screen.printLn( "Sincerely,\n" );
}
public string Name()
{
Screen.printLn( "Kevin Parkinson" );
}
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|