Advertisement
//C# Example of try-catch block
using System;
class TryCatch
{
public static void Main(String[] ar)
{
//Local variables
int a=10, b=0, c;
//Defining the try-catch block
try
{
//ArithmeticException i.e. Divide by zero exception is thrown here
c=a/b;
Console.WriteLine(c);
}
catch(DivideByZeroException exp) //matching ArithmeticExcecption is declared in "catch block"
{
//Printing the message associated to the caught exception
Console.WriteLine("We have an Exception - "+ exp);
}
Console.WriteLine("Outside try-catch block");
}
}
We have an Exception - System.DivideByZeroException: Attempted to divide by zero.
at TryCatch.Main(String[] ar)
Outside try-catch block
Advertisement
//C# Example of try-catch block
//where a catch block has specified an exception type which is a subclass of the thrown exception type.
using System;
class TryCatch
{
public static void Main(String[] ar)
{
//Local variables
int a=10, b=0, c;
//Defining the try-catch block
try
{
//DivideByZeroExceptio is thrown here, which is subclass of ArithmeticException type of exception
c=a/b;
Console.WriteLine(c);
}
catch(ArithmeticException exp) //Matching parent class type i.e. ArithmeticExcecption is declared in "catch block"
{
//Printing the message associated to the caught exception
Console.WriteLine("We have an Exception - "+ exp);
}
Console.WriteLine("Outside try-catch block");
}
}
We have an Exception - System.DivideByZeroException: Attempted to divide by zero.
at TryCatch.Main(String[] ar)
Outside try-catch block
//C# Example of mismatch of an exception thrown and an exception declared in the catch block
using System;
class TryCatch2
{
//Defining the Opr() method of the class
public void Opr()
{
try
{
int a = 100, b = 0;
//ArithmeticException is thrown here
Console.WriteLine(a/b);
Console.WriteLine("The Opr() method ends");
}
catch(NullReferenceException nullExp) //Non-matching exception-type is declared in this catch block
{
Console.WriteLine("We have an Exception - "+nullExp);
}
Console.WriteLine("Outside try-catch block");
}
//Defining the Main() method
public static void Main(String[] arr)
{
//Creating an object of the class
TryCatch2 ob = new TryCatch2();
try
{
//Enclosing the call to Opr() method in try block
ob.Opr();
}
catch(DivideByZeroException e)
{
Console.WriteLine("Exception thrown by the Opr() method is caught in the Main() method:");
Console.WriteLine(e);
}
}
}
Exception thrown by Opr() method is caught in Main() method:
System.DivideByZeroException: Attempted to divide by zero.
at TryCatch2.Opr()
at TryCatch2.Main(String[] arr)
//C# There should be no statement between a try-block" and its associated catch block
using System;
using System.IO;
class A
{
public static void Main(String[] arr)
{
try
{
//Initiating to read a file
FileStream f = new FileStream("C:\\file.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
int i = 0;
while ((i = f.ReadByte()) != -1)
{
Console.Write((char)i);
}
f.Close();
}
Console.WriteLine("Statement between try-catch"); //Statement between try-catch block leads to compile error
catch(Exception e)
{
Console.WriteLine(e);
}
}
}
A.cs(21,2): error CS1524: Expected catch or finally
cA.cs(22,51): error CS1003: Syntax error, 'try' expected
cA.cs(22,51): error CS1514: { expected
cA.cs(22,51): error CS1513: } expected
//C# We cannot declare try block alone.
using System;
using System.IO;
class B
{
public static void Main(String[] arr)
{
try
{
//Initiating to read a file
FileStream f = new FileStream("C:\\file.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
}
}
}
B.cs(14,2): error CS1524: Expected catch or finally
//C# Not specifying the identifier of the an exception type in the catch block.
using System;
using System.IO;
class A
{
public static void Main(String[] arr)
{
try
{
//Initiating to read a file
FileStream f = new FileStream("E:\\file.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
int i = 0;
while ((i = f.ReadByte()) != -1)
{
Console.Write((char)i);
}
f.Close();
}
catch(IOException) //Not declaring the identifier of the exception type.
{
Console.WriteLine("An IOException is thrown");
}
}
}
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement