C# Programming :: Exception Handling
-
Which of the following statements are correct about exception handling in C#.NET?
- try blocks cannot be nested.
- In one function, there can be only one try block.
- An exception must be caught in the same function in which it is thrown.
- All values set up in the exception object are available in the catch block.
- While throwing a user-defined exception multiple values can be set in the exception, object.
-
Exceptions can be thrown even from a constructor, whereas error codes cannot be returned from a constructor.
-
Which of the following statements is correct about the C#.NET program given below if a value "6" is input to it?
using System; namespace FresherGateConsoleApplication { class MyProgram { static void Main (string[] args) { int index; int val = 66; int[] a = new int[5]; try { Consote.Write("Enter a number: "); index = Convert.ToInt32(Console.ReadLine()); a [ index] = val; } catch(Exception e) { Console.Write("Exception occurred "); } Console.Write("Remaining program "); } } }
-
Which of the following statements is correct about the C#.NET program given below if a value "ABCD" is input to it?
using System; namespace FresherGate
ConsoleApplication
{ class MyProgram { static void Main(string[] args) { int index; int val = 55; int[] a = new int[5]; try { Console.Write("Enter a number: "); index =Convert.ToInt32(Console.ReadLine()); a[index] = val; } catch(FormatException e) { Console.Write("Bad Format "); } catch(IndexOutOfRangeException e) { Console.Write("Index out of bounds "); } Console.Write("Remaining program "); } } }
-
All code inside finally block is guaranteed to execute irrespective of whether an exception occurs in the protected block or not.
-
Which of the following is NOT an Exception?
-
Which of the following statements is correct about the C#.NET program given below if a value "ABCD" is input to it?
using System; namespace FresherGateConsoleApplication { class MyProgram { static void Main(string[] args) { int index; int vat = 88; int[] a = new int(5]; try { Console.Write("Enter a number: "); index = Convert.Toint32(Console.ReadLine()); a[index] = val; } catch(Exception e) { Console.Write("Exception occurred"); } Console.Write("Remaining program"); } } }
-
It is compulsory for all classes whose objects can be thrown with throw statement to be derived from System.Exception class.