也许这是一个愚蠢的问题,Exception是否接受全部?,例如IOException,NoSuchAlgorithmException,InvalidKeySpecException ...所以当我调用这个方法时:
public String exception(Exception e){
StringWriter errors = new StringWriter();
e.printStackTrace(new PrintWriter(errors));
return errors.toString();
}
它会接受所有异常并使用原始异常名称返回它们吗?
对所有人来说,对不起那个愚蠢的问题,我刚刚开始使用java并希望确保这一点。
答案 0 :(得分:1)
所有异常和错误的超类都是Throwable
。所以,我不知道你将如何使用你的方法,但是对于线程中的错误和异常处理,你应该使用这个:
try {
...
} catch (Throwable t) {
...
}
<强> UPD 即可。顺便说一句,没有人限制从Throwable
创建自己的异常并抛出它而不是Exception
后代:
throw new Throwable() { ... };
因此,您的方法不会处理此类异常......
答案 1 :(得分:0)
是的,您的代码可以处理所有e instanceof Exception
。
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,InvalidTreferencesFormatException,InvalidTargetObjectTypeException,IOException,JAXBException,JMException,KeySelectorException,LastOwnerException,LineUnavailableException,MarshalException,MidiUnavailableException,MimeTypeParseException,MimeTypeParseException,NamingException,NoninvertibleTransformException,NotBoundException,NotOwnerException,ParseException,Par serConfigurationException,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