我们不能期望用户始终输入正确的详细信息。但是,如果不正确或意外的输入处理不当,整个代码可能会崩溃或陷入无限循环。这是由于意外条件或输入而在执行程序时出现的问题。例如,当数字除以零时,结果是无限的。异常处理是告诉程序继续执行下一个代码块或在某些情况下提供定义的结果的方法。
可以使用以下四个关键字来完成异常处理。
通过定义异常处理程序,您可以使软件或代码免于很多麻烦。在可能出现异常的地方定义异常处理程序是一个很好的做法。
语法:
每当引发异常时,声明的方法都会借助 try 和 catch 关键字捕获异常。我们需要将此组合放在代码部分,预计会出现异常。这些代码称为受保护代码。您还可以为一个 try 关键字定义多个 catch 关键字。在内容的最后,代码的最后部分将被执行到,并且无论是否引发异常都会执行。
代码:
try { //Define the statement that could cause an exception. } Catch(ExceptionName secondException) { //error handling code } Finally { //define the statement that will be executed }
有许多预定义的类用于处理异常。 try 块覆盖了可能引发异常的代码部分,catch 确认捕获异常时要执行的操作。该块的最后部分定义了无论是否检测到异常都必须执行的操作,并且 throw 部分会显示消息(如果设置了任何消息)。
C# 中有很多类可以用来表示异常。所有类均派生自名为 System 的主类。例外。有几个类也是从 System.ApplicationException 和 System.SystemException 派生的。
异常源自 System.异常类。这是 C# 常见异常类的列表。
Exception | Description |
System.DivideByZeroException | handles the error when trying to divide a number by zero. |
System.NullReferenceException | handles the error when referring to an object which does not exist. |
System.InvalidCastException | handles the error when trying invalid casting. |
System.IO.IOException | All input-output error is handled. |
System.FieldAccessException | When trying to access unauthorized class |
Exception handling is done by try and catches block in C#. The try block in C# is used to place the code that may throw an exception. The exception is handled by the catch block.
Code:
using System; public class exceptionhandling { public static void Main(string[] args) { int a = 10; int b = 0; int x = a/b; //we are trying to divide the number with zero Console.WriteLine("other part of the code"); } }
Output:
Code
using System; public class ExExample { public static void Main(string[] args) { try { int a = 10; int b = 0; int x = a / b; } catch (Exception e) { Console.WriteLine(e); } Console.WriteLine("Rest of the code"); } }
Output:
It will show you the message regardless the exception is caught.
Code
using System; public class Exceptionhandling { public static void Main(string[] args) { try { int x = 5; int y= 0; int z = x / y; } catch (Exception obj) { Console.WriteLine(obj); } finally { Console.WriteLine("Time to execute finally block"); } Console.WriteLine("Other part of the code"); } }
Output:
Code
using System; public class ExceptionHandling { public static void Main(string[] args) { try { int p = 6; int q = 0; int r= p/q; } catch (NullReferenceException nullObject) { Console.WriteLine(nullObject); } finally { Console.WriteLine("Exception not handled. Now Finally section will be executed"); } Console.WriteLine("Other part of the code"); } }
Output:
The not only system defined, but we can also set our own exception. However, we need to inherit the code in order to get this done.
Code
using System; public class userdefinedInvalidAge : Exception { public userdefinedInvalidAge (String errorMessage) : base(errorMessage) { } } public class TestUserDefinedException { static void validateAge(int age) { if (age < 18) { throw new userdefinedInvalidAge("Sorry, Age must be greater than 18"); } } public static void Main(string[] args) { try { validateAge(12); } catch (userdefinedInvalidAge e) { Console.WriteLine(e); } Console.WriteLine("Rest of the code"); } }
Output:
At any place you think it might generate an error because of anything, exception handler should be used. It is essential that you use a catch statement and start from generic to a specific exception. Your entire software or code is at risk without proper exception handler.
以上是C# 中的异常处理的详细内容。更多信息请关注PHP中文网其他相关文章!