Created
April 23, 2012 15:56
-
-
Save lukaspili/2471854 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @author Lukasz Piliszczuk <lukasz.pili AT gmail.com> | |
*/ | |
public class ExceptionTraining { | |
public static void main(String[] args) { | |
int choice = 10; | |
try { | |
choice = Integer.valueOf("sdf"); | |
System.out.println("Foo"); | |
} catch (NumberFormatException e) { | |
System.out.println(choice); | |
} | |
// AFFICHE 10 | |
choice = 10; | |
try { | |
System.out.println("Foo"); | |
choice = Integer.valueOf("sdf"); | |
System.out.println("Bar"); | |
} catch (NumberFormatException e) { | |
System.out.println(choice); | |
} | |
// AFFICHE Foo 10 | |
try { | |
Integer.valueOf("sdf"); | |
} catch (NumberFormatException e) { | |
System.out.println("Foo"); | |
} catch (Exception e) { | |
System.out.println("Bar"); | |
} | |
// AFFICHE Foo | |
try { | |
Integer.valueOf("sdf"); | |
} catch (Exception e) { | |
System.out.println("Bar"); | |
} catch (NumberFormatException e) { | |
System.out.println("Foo"); | |
} | |
// Ne compile pas car Exception englobe NumberFormatException | |
try { | |
Integer.valueOf(null); | |
} catch (NumberFormatException e) { | |
System.out.println("Foo"); | |
} catch (NullPointerException e) { | |
System.out.println("Bar"); | |
} | |
// Affiche Foo | |
try { | |
Integer.valueOf("sdf"); | |
} catch (NullPointerException e) { | |
System.out.println("Bar"); | |
} | |
// Crash car l'exception NumberFormatException n'est pas catchée | |
try { | |
Integer.valueOf("sdf"); | |
} catch (Exception e) { | |
System.out.println("Bar"); | |
} | |
// Affiche Bar | |
try { | |
Integer.valueOf("sdf"); | |
} catch (Exception e) { | |
System.out.println("Foo"); | |
} finally { | |
System.out.println("Bar"); | |
} | |
// Affiche Foo Bar | |
try { | |
Integer.valueOf("10"); | |
} catch (Exception e) { | |
System.out.println("Foo"); | |
} finally { | |
System.out.println("Bar"); | |
} | |
// Affiche Bar | |
try { | |
return Integer.valueOf("10"); | |
} catch (Exception e) { | |
return 0; | |
} finally { | |
System.out.println("Bar"); | |
} | |
// Affiche Bar puis retourne 10 | |
try { | |
return Integer.valueOf("10"); | |
} finally { | |
System.out.println("Bar"); | |
} | |
// Affiche Bar puis retourne 10 | |
try { | |
return Integer.valueOf("sdf"); | |
} finally { | |
System.out.println("Bar"); | |
} | |
// Affiche Bar puis crash | |
try { | |
return Integer.valueOf("10"); | |
} catch (Exception e) { | |
return 0; | |
} finally { | |
return 99; | |
} | |
// Retourne 99 | |
try { | |
return Integer.valueOf("asd"); | |
} catch (Exception e) { | |
return 0; | |
} finally { | |
return 99; | |
} | |
// Retourne 99 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment