Throwing exceptions as well as catching exceptions?

I was wondering how Java accepts the following script

public static void main(String[] args) throws IndexOutOfBoundsException, CoordinateException, MissionException, SQLException, ParserConfigurationException { try { doSomething(); } catch (Exception e) { e.printStackTrace(); } } 

In the above code, I declare that the main function throws many different exceptions, but inside this function I catch a general exception. I am wondering how Java accepts this internally? For example, doSomething() throws an IndexOutOfBounds exception, e.printStackTrace () is called in the last catch (Exception e) {...} block?

I know if an exception not declared in the function's throwing area is thrown, try / catch will handle it, but what about the exceptions mentioned in the declaration?

+4
source share
5 answers

The catch block takes precedence over method-level throw declarations. If something passes this catch block, it will be selected by the method (but this is not the case, since all the mentioned exceptions really inherit from Exception ).

If you need an exception that will be handled by the catch block, but redirected further, you will have to rebuild it, for example.

 throw e; 
+3
source

In your case, if ANY Exception is doSomething() or doSomething() in doSomething() , it will be caught in the try-catch block due to the Exception e that you are catching.

Exception is the parent of all Exceptions. All exceptions are inherited from this class.

+5
source

say doSomething () throws an IndexOutOfBounds exception, will e.printStackTrace () be called in the last catch (Exception e) {...} block?

Yes, e.printStackTrace() will be called. Because you caught an Exception that is wider than (direct or indirect superclass) IndexOutOfBoundException or any other exception that you put in the throws clause. But if you catch some exceptions that are narrower than IndexOutOfBoundException and other exceptions in the throw clauses, and any of these exceptions occur, then the throw clause is called.

+1
source

Suppose you have this code:

  try{ doSomething(); }catch(Exception ex){ System.out.println("Exception:"); ex.printStackTrace(); }catch(IndexOutOfBoundsException ex){ System.out.println("IndexOutOfBoundsException :"); ex.printStackTrace(); } 

You will receive a compilation error

Exception IndexOutIfBounds already caught

This is because all exceptions are inherited from the Exception class, so the first thing that catches the blocking block is the exception, because IndexOutOfBounds is the exception, so you don't need to catch it twice one for the first and second for the second.
In one case, throws will do their job unless you declare an exception class after throws or in try-catch.

0
source

The exceptions mentioned in the declaration should allow the method further up the call stack to handle it. In your case, these exceptions, if your main method throws them, will be handled at runtime.

Source http://docs.oracle.com/javase/tutorial/essential/exceptions/declaring.html

0
source

Source: https://habr.com/ru/post/1488379/


All Articles