是否可以在不添加“抛出异常”的情况下抛出异常?

时间:2017-09-10 05:24:00

标签: java exception nullpointerexception illegalargumentexception

我发现很少有例外IllegalArgumentExceptionthrows Exception不需要添加throws Exception来抛出异常。为什么以及哪些类似的异常不需要添加public class WithoutThrowsException { public static void printSomething(String str) { if(str == null) throw new NullPointerException("Inputted string is null "); if(str.equals("rocket")) throw new IllegalArgumentException("Illegal argument exception "); } public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Hello World!!"); printSomething("rocket"); System.out.println("Mission accomplished !"); } }

@GetMapping(value="/cui",produces = MediaType.APPLICATION_XML_VALUE) 
public String cui() {
  String responseXML = // This is my final result object which is XML.
   return responseXML;
}

0 个答案:

没有答案