Error Handling in C# with Exceptions Flashcards

1
Q

Why handling errors? list 4 reasons

A
  • So the program doesnt crash.
  • Gives the chance to fix/retry.
  • meaningful message and graceful exit
  • opportunity to log erros
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why using exceptions instead of error codes?

A

more readable , exceptions can bubble up, can catch excptions at higher levels or sigle place… handle system errors… better documentable in the constructors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What an exception is made of?

A

Is an object that inherits from system.Exception and is created via throw statement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is an exception?

A

any error condition or unexpected behavior that is encountered by an executing program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

how exceptions are organized?

A

via an hierarchy of classes.

see here: https://pasteboard.co/JCvqsHk.png

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the properties exposed by an exception class?

A

Message: the explataion of what happened.
Stack trace: all the call chain.
Data: key value pair with extra data.
InnerException: Exception that caused the exception.
Source: application/object name that caused the error (assembly).
Help link: help information about the error;
Target site: method that threw the exception plus a lot of good information reflected.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the guidelines of an ApplicationException and SystemException class?

A

should not be thrown, extended or caught (unless you plan rethrowing it)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What should i do when I want to catch an OutOfMemoryException?

A

call Environment.FailFast to terminate the app and log to the console

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What happens when an exception is not handled by the application or framework?

A

the app will crash (stopped working).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to create try catch block that treats different exception types? Does the order matter?

A

multiple catches are defined… the order is from most specific to least specific. top down

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are some exception handling good practices?

A
  • catch blocks that swallow exceptions;
  • invalid inputs, for eg, is not and exceptional situation.. you need to validate (or code to handle that).
  • Design code to avoid exceptions.
  • Localize error messages (the exceptions itself).
  • use finally for cleanups.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly