什么是异常以及如何在应用程序中处理异常?

The exception is a term in applications development or programming. Simply exception is used to specify a state or a situation which is unexpected in applications. Exceptions occur in different and unexpected situations where there are a lot of different cases for an application and algorithm. The term also throwing exception by and applications also used for occurrences of the exceptions.

例外是应用程序开发或编程中的术语。 简单地,异常用于指定应用程序中意外的状态或情况。 异常发生在不同的意外情况下,其中应用程序和算法的情况很多。 该术语也throwing exception ,并且应用程序也用于发生异常。

Exceptions occur during runtime which means exceptions occurs during usage of the applications. This is different then compile-time errors which will simply prevent the applications compile and program and binary creation. This runtime behavior makes exceptions unexpected and problematic to the applications developers because generally they can not be defined during application development.

异常在运行时发生,这意味着异常在应用程序使用期间发生。 这与编译时错误不同,后者将仅阻止应用程序进行编译以及程序和二进制文件的创建。 这种运行时行为使异常对于应用程序开发人员来说是意外的和成问题的,因为通常无法在应用程序开发期间定义它们。

“引发,引发和处理异常”条款 (“Raising, Throwing and Handling An Exception” Terms)

There is 3 main term used with the exception which raising exception, throwing exception and handling exception.

raising exceptionraising exception throwing exceptionhandling exception有3个与异常一起使用的主要术语。

  • `Throwing Exception` means there is a case where an exception will be thrown by the application without the intent of the application. This occurs if the uncalculated sitıations and not an explicit exception creation by the applications or application developer.

    “抛出异常”表示在某些情况下,应用程序会在没有应用程序意图的情况下引发异常。 如果未计算的情况而不是由应用程序或应用程序开发人员创建的显式异常,则会发生这种情况。
  • `Raising Exception` means there is a programmer-defined exception situation and the exception should be raised explicitly. This type of exception cases is precalculated and coded into applications.

    “引发异常”表示存在程序员定义的异常情况,应显式引发异常。 这种类型的异常情况已预先计算并编码到应用程序中。
  • `Handling Exception` means managing the exception with different actions like ignoring it, logging it, taking another program into action. Exception handling is important which is defined for expected exception locations and handle the exception with different actions inside the program.

    处理异常意味着用不同的动作来管理异常,例如忽略它,记录它,使另一个程序生效。 异常处理很重要,它是为预期的异常位置定义的,并通过程序内部的不同操作来处理异常。

例外情况示例 (Example Exception Cases)

There is a lot of different type of exceptional cases. But some of them are very popular and faced a lot of time while applications development. Generally, exceptions are named with the library or class name of exception but in order to be simple and understandable, we will list them with the case name.

有很多不同类型的例外情况。 但是其中一些非常受欢迎,并且在应用程序开发时面临很多时间。 通常,使用异常的库或类名称来命名异常,但是为了简单易懂,我们将使用案例名称列出它们。

LEARN MORE  What Is Segmentation Faults and Causes?
了解更多什么是细分错误和原因?

除以零 (Dividing By Zero)

Dividing by zero is one of the most popular exceptions. This is actually a mathematical exception which is not logical. We can not divide any number into 0 because of its unlogical and equals to the infinite. This exception generally categorized under ArithmeticException in programming languages.

Dividing by zero是最流行的例外之一。 这实际上是数学上的异常,不是逻辑上的。 由于数字不合逻辑且等于无穷大,因此无法将任何数字均分为0。 此异常通常在编程语言中归类为ArithmeticException

访问数组中的未定义项 (Access To Undefined Item In Array)

Another popular exception type is trying to access or call or an unexisting item in an array. This exception is generally related with the using an index number which item does not exist. Think about we have an array named cars with 5 items but we call for 7th item which does not exist in cars arrays and will raise an exception.

另一种流行的异常类型是尝试访问或调用数组中不存在的项。 此异常通常与使用不存在该项的索引号有关。 考虑一下我们有一个名为cars的数组,其中包含5个项目,但是我们要求第7个项目不存在于cars数组中,这将引发异常。

car[5]={"hyundai" , "fiat" , "ferrari" , "skoda" , "renault"}

mycar = cars[7]   //This will raise an exception

转换不兼容的类型 (Convert Incompatible Types)

Programming languages provide some short cuts to the programmers and developers like converting some type into another type without writing excessive code. For example, we can convert “45.3” string type into a floating number type. But is the string contains non-numeric characters like “a”,”!” etc this will raise an exception named Convert Incompatible Type.

编程语言为程序员和开发人员提供了一些捷径,例如将某种类型转换为另一种类型而无需编写过多的代码。 例如,我们可以将“ 45.3”字符串类型转换为浮点数字类型。 但是字符串是否包含非数字字符,例如“ a”,“!” 等等,这将引发一个名为Convert Incompatible Type的异常。

mystringnumber="12.34abc"

mynumber = Convert(mystringnumber)  //will raise an exception

调用不存在的对象 (Call To Non-Existing Object)

Another exception type which occurs generally is Call To Non-Existing Object. This generally occurs when we call an unexisting object or function which is not properly initialized or created. For example Object.create() function does not return an object to the variable myobj and the usage of obj will rais an exception.

通常发生的另一种异常类型是“ Call To Non-Existing Object 。 通常,当我们调用一个不存在的,未正确初始化或创建的对象或函数时,就会发生这种情况。 例如,Object.create()函数不会将对象返回到变量myobj,并且obj的使用将引发异常。

myobj = Object.create()

myobjec.somemethod() // will raise an exception

Java中的异常 (Exceptions In Java)

Java programming language provides try , catchthrow keywords in order to manage exceptions.

Java编程语言提供trycatchthrow关键字以管理异常。

  • `try` is used to surround possible exception code block where there is a probability to occur an exception.

    “ try”用于包围可能发生异常的可能的异常代码块。
  • `catch` is used to specify instruction or commands run when previously defined `try` or exception block throws an exception.

    “ catch”用于指定在先前定义的“ try”或异常块引发异常时运行的指令。
  • `throw` simply used to raise an exception explicitly or manually.

    “ throw”仅用于显式或手动引发异常。
LEARN MORE  C# Programming Language Null Reference Exception and Solution
了解更多C#编程语言Null参考异常和解决方案

用户定义的异常(User-Defined Exception)

We can also create user-defined exceptions which are designed for special cases. The base class for a user-defined exception is Exception which will be extended. In this example, we will create an exception named PofException which will raised in our main application.

我们还可以创建针对特殊情况设计的用户定义的异常。 用户定义的异常的基类是Exception ,它将被扩展。 在此示例中,我们将创建一个名为PofException的异常,该异常将在主应用程序中引发。

// This is a user defined exception class
class PofException extends Exception 
{ 
    public PofException(String s) 
    { 
        // Call constructor of parent Exception 
        super(s); 
    } 
} 

//Program that will create or raise exception
public class Main 
{ 
    // Driver Program 
    public static void main(String args[]) 
    { 
        try
        { 
            // Throw an object of user defined exception 
            throw new PofException("Poftut.com"); 
        } 
        catch (PofException ex) 
        { 
            System.out.println("Exception Caught"); 

            // Print the message from MyException object 
            System.out.println(ex.getMessage()); 
        } 
    } 
}
 

翻译自: https://www.poftut.com/what-is-exception-and-how-to-handle-an-exception-in-applcations/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值