zoukankan      html  css  js  c++  java
  • java异常

    异常

    Exception继承于 java.lang.throwable;被称为异常。Exception是Throwable的一个主要子类。Exception下面还有子类:编译时异常:Java.lang.Exception;运行期异常:Java.lang.RuntimeException。

    什么叫编译时异常?

    javac将源代码编成字节码的时候报的错。程序正确,但因为外在的环境条件不满足引发。例如:用户错误及I/O问题----程序试图打开一个并不存在的远程Socket端口。这不是程序本身的逻辑错误,而很可能是远程机器名字错误(用户拼写错误)。对商用软件系统,程序开发者必须考虑并处理这个问题。Java编译器强制要求处理这类异常,如果不捕获这类异常,程序将不能被编译。

    什么叫运行期异常?

    eclipse或者as在写java过程中 ,编译器报错的错误。这意味着程序存在bug,如数组越界,0被除,入参不满足规范.....这类异常需要更改程序来避免,Java编译器强制要求处理这类异常。

    Error也继承于 java.lang.throwable;被称为错误。Error用来处理程序运行环境方面的异常,比如,虚拟机错误、装载错误和连接错误,这类异常主要是和硬件有关的,而不是由程序本身抛出的

    异常的捕捉:

    try{}...catch(异常类名 变量名){}

    try{}...catch(异常类名 变量名){}...catch(异常类名 变量名){}

    try{}...catch(异常类名 变量名){}...catch(异常类名 变量名){}...finally{}

    try{}...finally{}

    try必须至少与finally或catch合用。

    抛出异常:

    import java.io.*;

    public class className { public void deposit(double amount) throws RemoteException

    {

    // Method implementation

    throw new RemoteException();

    } //Remainder of class definition}

    }

    import java.io.*;

    public class className

    {

    public void withdraw(double amount) throws RemoteException, InsufficientFundsException

    {

    // Method implementation

    }

    //Remainder of class definition

    }

  • 相关阅读:
    Neko's loop HDU-6444(网络赛1007)
    Parameters
    SETLOCAL
    RD / RMDIR Command
    devenv 命令用法
    Cannot determine the location of the VS Common Tools folder.
    'DEVENV' is not recognized as an internal or external command,
    How to change Visual Studio default environment setting
    error signing assembly unknown error
    What is the Xcopy Command?:
  • 原文地址:https://www.cnblogs.com/vitabebeauty/p/7232896.html
Copyright © 2011-2022 走看看