TempIsZeroException: Temperature level is 0!
// commands may cause an exception (exception)
} catch (foreign_folder_name e1) {
// code to handle errors
} catch (foreign_folder_name e2) {
// code to handle errors
} catch (foreign_folder_name) {
// code to handle errors
} finally {
// commands to be executed
}
You can list many catch commands to catch different exception types in case your try block appears more than one exception in different situations.
Exceptions in C # are represented by classes. The Exception classes in C # are primarily inherited directly or not directly from the System.Exception class in C #. Some Exception classes inherit from the System.Exception class are System.ApplicationException and System.SystemException classes.
The System.ApplicationException class supports the exceptions created by application programs. Therefore, the exceptions defined by the programmer should inherit from this class.
The System.SystemException class is the base class for all pre-defined system exception.
The following table provides some classes of pre-defined Exception inherited from the Sytem.SystemException class in C # :
Exception Class DescriptionSystem.IO.IOException Handling error I / O. System.IndexOutOfRangeException Handling errors created when a method references an index outside the array array. System.ArrayTypeMismatchException Handling errors created when type is not suitable for array type. System.NullReferenceException Handling errors created from referencing a null object. System.DivideByZeroException Handling errors created when dividing by 0. System.InvalidCastException Error handling generated during casting. System.OutOfMemoryException Error handling is created from the lack of free memory. System.StackOverflowException Error handling generated from stack overflow.C # provides a highly structured solution for exception handling in the form of try and catch blocks. Using these blocks, the main commands of the program are separated from the error handling commands in C #.
These exception handling blocks are implemented by using try, catch and finally keywords in C #. For example, throw an exception when dividing by 0.
using System ; namespace VdXuLyNgoaiLe { class PhepChia { int result ; PhepChia () { result = 0 ; } public void chia ( int so1 , int so2 ) { try { result = so1 / so2 ; } catch (DivideByZeroException e ) { Console . WriteLine ( "Exception caught: {0}" , e ); } finally { Console . WriteLine ( "Kết quả: {0}" , result ); } } static void Main ( string [] args ) { PhepChia d = new PhepChia (); d . chia ( 25 , 0 ); Console . ReadKey (); } } }
When compiling and running the above C # program will produce the following results:
Exception caught: System.DivideByZeroException: Attempted to divide by zero.
at VdXuLyNgoaiLe.PhepShare.chia (System.Int32 so1, System.Int32 so2) [0x00000] in: 0
Result: 0
You can also define exceptions. The User-Defined Exception classes are inherited from the ApplicationException class in C #. The following example illustrates this:
Creating 3 classes named in turn is as follows:
Temperature class
using System ; namespace VdDinhNghiaException { class KtraNhietDo { static void Main ( string [] args ) { NhietDo nhiet = new NhietDo (); try { nhiet . xemNhietDo (); } catch ( TempIsZeroException e ) { Console . WriteLine ( "TempIsZeroException: {0}" , e . Message ); } Console . ReadKey (); } } } public class TempIsZeroException : Exception { public TempIsZeroException ( string message ): base ( message ) { } } public class NhietDo { int temperature = 0 ; public void xemNhietDo () { if ( temperature == 0 ) { throw ( new TempIsZeroException ( "Mức nhiệt độ bằng 0!" )); } else { Console . WriteLine ( "Nhiệt độ: {0}" , temperature ); } } }
Compiling and running the above C # program will produce the following results:
TempIsZeroException: Temperature level is 0!
You can throw an object if it: either directly or indirectly inherits from the System.Exception class in C #. You can use a throw command in the catch block to throw that presence object:
Catch (Exception e)
{
.
Throw e
}
According to Tutorialspoint
Last lesson: Regular Expression in C #
Next article: File I / O in C #