简体   繁体   中英

C# Try/Catch statement, am i doing this right?

using System;

namespace Something
{
    class MainProgram
    {
        public static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Hello? am I working?")
            }
            catch
            {
                Console.WriteLine("Nah I ain't working");
            }
        }
    }
}

I'm just poking about with C#, and I wanted to try errorchecking and whatnot. There's the code and the console. Does Catch{} just not do syntax errors?

Catch only catches runtime errors. This can be seen as you specify what it will catch and there is no type you specify in the catch that represents syntax errors.

If there is a syntax error, there is no program that runs the catch in the first place. The syntax error is issued by the compiler before the program exists and then is run, this is because C# is a compiled language. An editor or IDE can give you an early heads-up as well, but they also don't run your program to issue those errors.

And yes, you can use a catch without specifying the type of Exception , but that's just a shortcut for catching all errors which derive from Exception so it is a shortcut for catch (Exception e)

In other languages, such as JavaScript, which is an interpreted language, the interpreter (as opposed to a compiler) runs JavaScript code until it finds an error. So you can possibly have a partially run program that has syntax errors in it.


JavaScript example, just for fun:

try { eval('func);'); } catch (e) { console.log('The error is a syntax error: ', e instanceof SyntaxError); }

This will output "The error is a syntax error: true" because it tries to run func); which is syntactically malformed. The SyntaxError was actually issued during runtime (remember, this is JavaScript not C#).

Try/Catch is not for SyntaxErrors, but Runtime errors. You can do

try{
  // Code here
} catch(Exception e){
  // log the exception
} finally{
  // code here will run no matter what (with or without errors)
}

As you can see, catch can optionally take a Exception parameter that can help you log it better. In your case, you will catch all exceptions, but you don't have a reference to the exception.

Try catch is usually used for cases like reading from a file/database, making web requests, etc. All these things may fail for external reasons but they are Syntax-wise correct.

This has already been answered but just to give you confidence the others are correct yes.

You typically will try to do something for example simply call a method or do some complicated logic you want to make sure doesn't exit out so you would write:

try{ XYZ } catch(Exception e){}

if you know the kind of exception that you are going to encounter whether it be a argumentoutofrange exception or nullpointerexception or anything specific like that you can change the catch to focus on that type.

The general Exception type is okay for working out issues when you don't know exactly what the problem is but I would say find out what the exception is and cater your response to best deal with it.

Sorry this was very wordy I just think understanding is more important since you seem new.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM