简体   繁体   English

例外是否对所有例外有效?

[英]Exceptions is valid for all exceptions?

Maybe this is a stupid question, did Exception accept all?, for example IOException, NoSuchAlgorithmException,InvalidKeySpecException... so when i call this method: 也许这是一个愚蠢的问题,Exception是否接受所有内容?例如IOException,NoSuchAlgorithmException,InvalidKeySpecException ...因此,当我调用此方法时:

public String exception(Exception e){
    StringWriter errors = new StringWriter();
    e.printStackTrace(new PrintWriter(errors));
    return errors.toString();
}

it will accept all exceptions and return them with the original exception name? 它会接受所有异常并以原始异常名称返回它们吗?

Thx for all, and sorry for that stupid question i just started java and want to make sure that point. 谢谢大家,对于这个愚蠢的问题,我刚刚启动Java,并希望确保这一点。

The super class for all exceptions and errors is Throwable . 所有异常和错误的超类都是Throwable So, I don't know how you will use your method, but for error&exception handling in threads, for example, you should use this one: 因此,我不知道如何使用您的方法,但是例如,对于线程中的错误和异常处理,您应该使用以下方法:

try { ... } catch (Throwable t) { ... }

UPD . UPD BTW, nobody restricts create own exception from Throwable and throw it instead of Exception descendants: 顺便说一句,没有人限制从Throwable创建自己的异常并抛出它而不是Exception后代:

throw new Throwable() { ... };

So, you method will not process such exceptions... 因此,您的方法将不会处理此类异常...

Yes, your code can handle all e instanceof Exception . 是的,您的代码可以处理所有e instanceof Exception

Exception is the base class of all exceptions, there is a list of direct subclasses in the JavaDocs : Exception是所有异常的基类, JavaDocs中有直接子类的列表:

AclNotFoundException, ActivationException, AlreadyBoundException, ApplicationException, AWTException, BackingStoreException, BadAttributeValueExpException, BadBinaryOpValueExpException, BadLocationException, BadStringOperationException, BrokenBarrierException, CertificateException, CloneNotSupportedException, DataFormatException, DatatypeConfigurationException, DestroyFailedException, ExecutionException, ExpandVetoException, FontFormatException, GeneralSecurityException, GSSException, IllegalClassFormatException, InterruptedException, IntrospectionException, InvalidApplicationException, InvalidMidiDataException, InvalidPreferencesFormatException, InvalidTargetObjectTypeException, IOException, JAXBException, JMException, KeySelectorException, LastOwnerException, LineUnavailableException, MarshalException, MidiUnavailableException, MimeTypeParseException, MimeTypeParseException, NamingException, NoninvertibleTransformException, NotBoundException, NotOwnerException, ParseException, Parser AclNotFoundException,ActivationException,AlreadyBoundException,ApplicationException,AWTException,BackingStoreException,BadAttributeValueExpException,BadBinaryOpValueExpException,BadLocationException,BadStringOperationException,BrokenBarrierException,CertificateException,CloneNotSupportedException,DataFormatException,DatatypeConfigurationException,DestroyFailedException,ExecutionException,FormatException,FormatInterException,FormatException, InvalidMidiDataException,InvalidPreferencesFormatException,InvalidTargetObjectTypeException,IOException,JAXBException,JMException,KeySelectorException,LastOwnerException,LineUnavailableException,MarshalException,MidiUnavailableException,MimeTypeParseException,MimeTypeParseException,NamingException,NoninvertibleTransformException,NotBoundException,Owner ConfigurationException, PrinterException, PrintException, PrivilegedActionException, PropertyVetoException, ReflectiveOperationException, RefreshFailedException, RemarshalException, RuntimeException, SAXException, ScriptException, ServerNotActiveException, SOAPException, SQLException, TimeoutException, TooManyListenersException, TransformerException, TransformException, UnmodifiableClassException, UnsupportedAudioFileException, UnsupportedCallbackException, UnsupportedFlavorException, UnsupportedLookAndFeelException, URIReferenceException, URISyntaxException, UserException, XAException, XMLParseException, XMLSignatureException, XMLStreamException, XPathException ConfigurationException,PrinterException,PrintException,PrivilegedActionException,PropertyVetoException,ReflectiveOperationException,RefreshFailedException,RemarshalException,RuntimeException,SAXException,ScriptException,ServerNotActiveException,SOAPException,SQLException,TimeoutException,TooManyListenersException,TransformerException,TransformException,UnsupportededException,UnsupportedSupportedException, UserException,XAException,XMLParseException,XMLSignatureException,XMLStreamException,XPathException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM