Advertisement
//C# If an exception is not thrown out of try block, finally block still runs.
using System;
class A
{
public static void Main(String[] ar)
{
try
{
A ob = new A ();
}
catch(Exception e)
{
Console.WriteLine( "Exception caught"+e);
}
finally
{
Console.WriteLine("A finally block is executed irrespective of an exception is thrown or not");
}
}
}
A finally block is executed irrespective of an exception is thrown or not
//C# The finally block runs even when a thrown exception is caught by a catch block
using System;
class Class1
{
static void Main(string[] args)
{
//Creating an String array
String[] names = {"Tomato", "Apple", "Cranberries"};
//Casting String array to an Object array
Object[] obArr = (Object[]) names;
//Upcasting an int to Object
Object obj = (Object) 13;
try
{
//Trying to assign an int(casted to an Object)
// to an String array(casted to an Object array)
obArr[2] = obj;
}
catch(ArrayTypeMismatchException)
{
Console.WriteLine("An ArrayTypeMismatchException is caught");
}
finally
{
Console.WriteLine("A finally block is executed even after an exception is caught in the catch-block");
}
}
}
An ArrayTypeMismatchException is caught
A finally block is executed even after an exception is caught in the catch-block
Advertisement
using System;
class A
{
public static void Main(String[] ar)
{
try
{
//Assigning a null reference to a reference.
A ob = null;
//Calling ToString() on null reference throws NullPointerException
Console.WriteLine(ob.ToString());
}
catch(ArithmeticException e)
{
Console.WriteLine("Exception caught - " + e);
}
finally
{
Console.WriteLine("A finally block is executed even after an exception is not caught in the catch-block");
}
}
}
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
at A.Main(String[] ar)
A finally block is executed even after an exception is not caught in the catch-block
// C# The finally block runs even in the absence of a catch block
using System;
class A
{
public static void Main(String[] ar)
{
try
{
//Assigning a null reference
A ob = null;
//Calling ToString() method on a null reference, which throws a NullReferenceException
Console.WriteLine(ob.ToString());
}
finally
{
Console.WriteLine("A finally block is executed in the absence of catch-block, when exception is thrown.");
}
}
}
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
at A.Main(String[] ar)
A finally block is executed in the absence of catch-block, when exception is thrown.
//C# Example of finally block, which always runs
using System;
class A
{
public static void main(String... ar)
{
try
{
//Creating an object of class A
A ob = new A();
}
finally
{
Console.WriteLine("A finally block is executed in the absence of catch-block, when exception is not thrown.");
}
}
}
A finally block is executed in the absence of catch-block, when exception is not thrown.
using System;
using System.IO;
class FinallyEx
{
public static void Main(String[] ar)
{
FileStream f = null;
try
{
//Creating a file stream
f = new FileStream("E:\\File.txt", FileMode.OpenOrCreate);
//writing a byte to the stream
f.WriteByte(65);
//Flushing the data to a file
f.Flush();
}
catch(IOException e)
{
Console.WriteLine("Exception caught: " + e);
}
finally
{
try
{
//Cleaning up the resources i.e. closing an opened file.
f.Close();
}
catch(IOException e)
{
Console.WriteLine(e);
}
}
}
}
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement