Try catch

Ace your homework & exams now with Quizwiz!

Which of the following commands would correctly open a file named info.txt for reading

File fileIn = new File("info.txt");

What java exception may occur when opening a file?

FileNotFoundException

What would happen if the file myFile.txt is not found in the following code segment? try File f = new File("myFile.txt"); } catch(FileNotFoundException e){ System.out.println("File Not Found"); } catch(Exception e){ System.out.println("General Error"); }

It will display the message "File Not Found"

What would be printed by the dangerous code segment? try{ int num[] = {1, 2, 3}; num[-1] = 5; } catch(ArrayIndexOutOfBoundsException e){ System.out.println("One"); } catch(ArithmeticException e){ System.out.println("Two"); } catch(Exception e){ System.out.println("Three"); }

One

Which two java objects can be used to write to a file?

PrintWriter and FileWriter

How must the following pathname be put into the string in order for the Java compiler to read it? C:\Users\Java\Data.txt

String fileName = "C:\\Documents\\Java\\Data.txt";

What would happen if the user tried to enter the string "ten" into the following nextInt() statement? int num; try{ num = in.nextInt(); } catch(InputMismatchException e){ String str = in.next(); System.out.println(str); }

The variable num would not be defined. It would run the catch statement . The value "ten" would be assigned to str.

What method of an Exception object returns a string of the error message?

getMessage()

Which of the following is the correct way to formulate the try/catch statement?

int num = 0; try{ num += 5 }catch(ArithmeticExpression e){ System.out.print("error"); }

Which of the following would create an Exception in Java?

int num = 42; int value = 0; System.out.println(num / value);


Related study sets

Periodic Table Of The Elements: 1-20

View Set

River Valley Civilization Study Guide - World History

View Set

diathesis-stress model of stress illness

View Set

AP Psychology Research Methods Review

View Set

PN NCLEX 6th Edition- Mental Health

View Set

Google Ads Search (AdWords) Certification 2020

View Set