exception handling - C# try-catch-else -


One thing that has bugged me with the exception handling coming from c # from C # is that any of the C # It does not seem to be the way of specifying another section For example, in Python I can write something like this (note, this is just an example.) I am not saying what is the best way to read the file ):

  try {reader = new streamrider (path); } Hold (exception) {// uh oh something went wrong to open the file for reading} and {string line = reader.ReadLine (); Character = line [30]; }  

I have seen the people with some C # code, they will write the following:

  try {reader = new streamReader (path); String line = reader Readline (); Character = line [30]; } Hold (exception) {Uh oh something went wrong, but where? }  

The problem with this is that I do not want to capture the range exception coming from the fact that the file can not contain more than 30 characters in the first line. I just want to capture the exceptions related to reading the file stream. Is there any similar composition that I can use to get the same thing in C #?

Hold exception to a specific class

  try {reader = New streamrider (path); String line = reader Readline (); Character = line [30]; } Got (IOException pre) {// Uh oh was wrong with some I / O} (Exception ex) {// Uh oh something went wrong; // Unless you know what you are doing here)  

The second cat is optional, of course. And since you do not know what happened, it is very dangerous to swallow this most common exception.


Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -