Exception Handling

advertisement
EXCEPTIONS TECHNICAL QUESTIONS
1.What is an Exception?
An exception is an error that occurs during program execution. Generally, an exception describes
an unexpected event. For example, an exception will occur if a program requests more memory
than the operating system can provide. This exception is known as an Out of Memory
Exception.
2.Exception Handling in C#?
Exception handling is an in built mechanism in .NET framework to detect and handle run time errors.
The .NET framework contains lots of standard exceptions. The exceptions are anomalies that occur
during the execution of a program. They can be because of user, logic or system errors. If a user
(programmer) do not provide a mechanism to handle these anomalies, the .NET run time environment
provide a default mechanism, which terminates the program execution.
4.Why is it a bad idea to throw your own exceptions?
Well, if at that point you know that an error has occurred, then why not write the proper code to handle
that error instead of passing a new Exception object to the catch block? Throwing your own exceptions
signifies some design flaws in the project.
5.What is the purpose of the finally block?
The code in finally block is guaranteed to run, irrespective of whether an error occurs or not. Critical
portions of code, for example release of file handles or database connections, should be placed in the
finally block.
6.How do you handle errors in C#?
C# and VB.NET use structured error handling (unlike VB6 and earlier versions where error handling
was implemented using Goto statement). Error handling in both VB.NET and C# is implemented using
Try..Catch..Finally construct (C# uses lower case construct – try…catch…finally).
7.Describe how exceptions are handled by the CLR?
Usually the exceptions that occur in the try are caught in the catch block. The finally is used to do all
the cleaning up work. But exceptions can occur even in the finally block. By using CLR, the exception
are caught even in the finally block. Also when an exception is thrown, the CLR looks for an
appropriate catch filter that can handle the exception and then it executes the finally block before
terminating the execution on the catch filter.
8.Does C# have a 'throws' clause?
No, unlike Java, C# does not require (or even allow) the developer to specify the exceptions that a
method can throw.
9.When should I throw an exception?
This is the subject of some debate, and is partly a matter of taste. However, it is accepted by many that
exceptions should be thrown only when an 'unexpected' error occurs. How do you decide if an error is
expected or unexpected? This is a judgement call, but a straightforward example of an expected error is
failing to read from a file because the seek pointer is at the end of the file, whereas an example of an
unexpected error is failing to allocate memory from the heap.
10.what is the difference between finally and finalize() ?
finally:- It is a block associated with try catch to maintain cleanup code. Finally block will be executed always
irrespective of whether exception is raised or not raised or whether the exception is handle or not handle.
finalize():- It is a method, Garbage collector always calls this method just before destroying any object to
perform cleanup activities.
11.Difference between "throw" and "throw ex" in .NET?
If you use "throw" statement, it preserve original error stack information.
If you use"throw ex" statement, stack trace of the exception will be replaced with a stack trace starting
at the re-throw point.
12.What is the difference between system exceptions and application ?
System.SystemException -> This class is for exceptions that r usually thrown by the .net runtime, or
which r considered to be of a generic nature and must be thrown by almost any application. For
example, StackOverflowException will be thrown by the .net runtime if it detects the stack is full.
System.ApplicationException-> This class is important, becoz it is the intended base for any class of
exception defined by third parties. Hence, if u define any exceptions covering error conditions unique
to ur application, u should derive these directly or indirectly from System.ApplicationException.
13.A try block having 4 catch block will fire all catch block or not?
No, A try having more than one catch block will fire the first relevant catch block after that cursor will
be moved to the finally block (if exists) leaving all remaining catch blocks.
So in all cases only one catch block will fire.
14.Can we have try block without catch?
Yes,We can write Try { } Finally { } block. In this case exception will be thrown in try block if it is
but code inside finally block will execute.
15.can we write return statement in try catch or finally block.?
Yes,We can write the return statement in try catch & finally. The only thing is that even if we write
return statement in try bloack or catch block the finally block will always get executed.The only case in
which finally block does not execute is when we write System.Environment.Exit(0) in either try or
catch.
16.Explain Try/catch block of exception handling.?
You can enclose code in Try/Catch/Finally block. You can catch all exceptions in the catch block. The
third part of this block is finally. It is executed irrespective of the fact that an exception has been raised.
17.When can you use tracing with exception handling?
You can use tracing with exception handling to log unanticipated exception to the trace log. The log
file can be used to diagnose unanticipated problems and thus can be corrected.
18.What are the two diff types of error?
Run time error and Compile time error.
19. What is the purpose of throw?
In C#, it is possible to explicitly throw an exception, from the program. The throw keyword is used for doing
that. The general form of throwing an exception is as follows.
System.Exception exception_obj = new ArgumentException("This is my Exception message");
throw exception_obj;
or
throw new ArgumentException("This is my Exception message");
20.What are different types of exception properties ?
Helplink-- provide link to help files which give more info on the exception raised.
Message--gives the text that describes the exception.
Suppose
catch(Exception e)
{
throw new Exception("Failure")
}
then e.Message will give output as "Failure".
Source--provides the name of assembly from where exception is raised.
Target Site--name of method that throws an error
Stack Trace--provides stack flow of the exception.
Inner Exception--When exception is thrown from one catch to another the message
is passed as inner exception to second catch.
from first catch
21.Is it mandatory for a piece of code to have catch or finally when try block is there ?
Yes. If a try block is there then either catch or finally has to be there or else a compiler error is
generated.
22.Does finally get executed if the code throws an error ?
Finally is always executed .
23. How to throw a custom exception?
The usual try - catch - finally - entry format has to be followed. However, in this case, instead of using
the preset exceptions from the System. Exception, you define your own Exception class and inherit
Exception or Application Exception class.
You need to define three constructors in your Own Exception Class: One without parameters, other
with String parameter for error message and the last one has to have one parameter as a String and
other as an Inner exception object.
24: Can we get Exception in finally block?
yes, we may get.
25: In which assembly System namespace is there?
mscorlib.dll assembly contain system namespace
26. What is the purpose of Exception Handling?
An exception can occur anytime during the execution of an application. Your application must be prepared to
face such situations. An application will crash if an exception occurs and it is not handled.
"An exception handler is a piece of code which will be called when an exception occurs.
27. Is finally block will be execute always?
Yes finally block will be executed always irrespective of whether exception raised or not raised whether
exceptions are handled or not handle. There is one situation where the finally block won’t be executed if the
CLR is going to be shutdown.
28. If return statement present inside try is finally block will be executed?
Ans. Yes, if return statement present inside try, then also finally block will be executed. finally block will
dominate return statement also.
29. Is it possible to write any statement between try-catch and finally?
Ans. No, it is not possible to write any statement between try catch and finally. If we will try to write
any statement between them then we will get compile time error.
30. After throw is it allow to take any statement directly?
Ans. After throw statement we are not allow to place any statement directly violation leads to compile
time error saying Unreachable Statements.
31.If no exception handling is provided in the program, what will happen ?
If an exception is not 'handled' in code, the application will crash and user will see an ugly message.
Instead, you can catch the exception, log the errors and show a friendly message to the user.
32.What is Inner Exception?
The InnerException is a property of an exception. When there are series of exceptions, the most current
exception can obtain the prior exception in the InnerException property.
Let us say we have an exception inside a try block throwing an ArgumentException and the catch clause
catches it and writes it to a file. However, if the file path is not found, FileNotFoundException is thrown.
Let's say that the outside try block catches this exception, but how about the actual ArgumentException
that was thrown? Is it lost? No, the InnerException property contains the actual exception. This is the
reason for the existence of an InnerException property for any exception.
Download