How to catch all checked exceptions (in one block) in Java?

EDIT: This question only applies to Java 1.6 (and below). Apologies for not understanding this in the original post.

The hierarchy here shows that Java Exception divided into two types: RuntimeException and not RuntimeException:

Java Exception Hierarchy

Maybe something missed something, but would it be better to split it into something like UncheckedException and CheckedException ? For example, in the following statement there are several checked exceptions:

 try { transaction.commit(); } catch (SecurityException e) { } catch (IllegalStateException e) { } catch (RollbackException e) { } catch (HeuristicMixedException e) { } catch (HeuristicRollbackException e) { } catch (SystemException e) { } 

I'm really only interested in whether this succeeds or not, so I would like to deal with checked exceptions as a group, but not with unchecked exceptions, since it is not a good practice to catch an unexpected error. Therefore, keeping this in mind, perhaps I could do something like:

 try { transaction.commit(); } catch (Exception e) { if (e instanceof RuntimeException) { // Throw unchecked exception throw e; } // Handle checked exception // ... } 

But that seems horribly hacks. Is there a better way?

+6
source share
3 answers

If I understand correctly, then you are almost there. Just catch a RuntimeException. This will throw a RuntimeException and everything below it in the hierarchy. Then a breakthrough for exclusion, and you are covered:

  try {
     transaction.commit ();
 } catch (RuntimeException e) {
     // Throw unchecked exception
     throw e;
 } catch (Exception e) {
     // Handle checked exception
     // ...
 }
+6
source

Java 7 allows you to create such constructs:

 try { transaction.commit(); } catch (SecurityException | IllegalStateException | RollbackException | HeuristicMixedException e ) { // blablabla } 

UPD: I think that in earlier versions of Java there is no good and convenient way to do this. That is why Java developers have made such a design in Java 7 . So, you can develop your own approaches for Java 6 .

+5
source

I gave a similar answer in another post, so yes, this is a copy of the past:

Something I do is to have a static method that handles all exceptions, and I add a log to JOptionPane to show it to the user, but you can write the result to a file in FileWriter that was corrupted in BufeeredWriter . For the main static method, to catch Uncout exceptions, I do:

 SwingUtilities.invokeLater( new Runnable() { @Override public void run() { //Initializations... } }); Thread.setDefaultUncaughtExceptionHandler( new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException( Thread t, Throwable ex ) { handleExceptions( ex, true ); } } ); 

As for the method:

 public static void handleExceptions( Throwable ex, boolean shutDown ) { JOptionPane.showMessageDialog( null, "A CRITICAL ERROR APPENED!\n", "SYSTEM FAIL", JOptionPane.ERROR_MESSAGE ); StringBuilder sb = new StringBuilder(ex.toString()); for (StackTraceElement ste : ex.getStackTrace()) { sb.append("\n\tat ").append(ste); } while( (ex = ex.getCause()) != null ) { sb.append("\n"); for (StackTraceElement ste : ex.getStackTrace()) { sb.append("\n\tat ").append(ste); } } String trace = sb.toString(); JOptionPane.showMessageDialog( null, "PLEASE SEND ME THIS ERROR SO THAT I CAN FIX IT. \n\n" + trace, "SYSTEM FAIL", JOptionPane.ERROR_MESSAGE); if( shutDown ) { Runtime.getRuntime().exit( 0 ); } } 

In your case, instead of β€œscreaming” to the user, you can write a log, as I told you before:

 String trace = sb.toString(); File file = new File("mylog.txt"); FileWriter myFileWriter = null; BufferedWriter myBufferedWriter = null; try { //with FileWriter(File file, boolean append) you can writer to //the end of the file myFileWriter = new FileWriter( file, true ); myBufferedWriter = new BufferedWriter( myFileWriter ); myBufferedWriter.write( trace ); } catch ( IOException ex1 ) { //Do as you want. Do you want to use recursive to handle //this exception? I don't advise that. Trust me... } finally { try { myBufferedWriter.close(); } catch ( IOException ex1 ) { //Idem... } try { myFileWriter.close(); } catch ( IOException ex1 ) { //Idem... } } 

Hope I helped.

To have a good day.:)

0
source

All Articles