Advertisement
CotaIgnorada

excepciones

May 18th, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. package ej3;
  2. /*
  3. ejemplo 3:
  4.  
  5. import java.util.Scanner;
  6. import java.util.InputMismatchException;
  7.  
  8. public class Ej3{
  9.  
  10. public static void main(String[] args) {
  11. boolean ctr = true;
  12. while(ctr){
  13. try{
  14. Scanner r = new Scanner(System.in);
  15. int i = r.nextInt();
  16. ctr = false;
  17. }catch(InputMismatchException ex){
  18. ex.printStackTrace();
  19. }
  20. }
  21. }
  22. }
  23. - se puede tener muchos catch anidados
  24.  
  25. ****************************
  26.  
  27. se puede poner un solo catch que vea todas las exception
  28.  
  29. ej: catch(exception ex){
  30. ...
  31.  
  32. *********************************************
  33.  
  34. ** propagacion de exception **
  35.  
  36. public void doAlgo()throws InputMismatchException{ <-
  37. Scanner s = new Scanner (System.in);
  38. int i = s.nextInt();
  39. }
  40. public void doAlgoMas()throws InputMismatchException{
  41. doAlgo();
  42. }
  43. public void doAlgo3(){
  44. try{
  45. doAlgoMas();
  46. }catch(InputMismatchException ex){
  47. ...
  48. }
  49. }
  50. *************************************
  51. try{
  52. ...
  53. }catch(... ex){
  54. ...
  55. }catch(... ex2){
  56. ...
  57. }finally{ <- se puede cerrar el archivo
  58. try{
  59. ...
  60. }catch(...)
  61. }
  62.  
  63. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement