d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Exception Handling Java
12Next
Add Reply New Topic New Poll
Member
Posts: 1,081
Joined: Aug 25 2013
Gold: Locked
Trader: Scammer
Nov 18 2013 10:19pm
I need to throw an exception if an empty string is entered.

meaning the user just presses enter rather than entering the string that they were supposed to.

What exception would I be throwing if they just press enter where they are supposed to enter in their input?
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 18 2013 10:22pm
InvalidArgument or a custom one that inherits RuntimeException

or go the lazy way and throw NullPointerException

the user isn't going to see it regardless. i imagine you'll catch it and show a nice error message.

This post was edited by carteblanche on Nov 18 2013 10:23pm
Member
Posts: 1,081
Joined: Aug 25 2013
Gold: Locked
Trader: Scammer
Nov 18 2013 10:26pm
Quote (carteblanche @ Nov 18 2013 11:22pm)
InvalidArgument or a custom one that inherits RuntimeException

or go the lazy way and throw NullPointerException

the user isn't going to see it regardless. i imagine you'll catch it and show a nice error message.


right now I have this:

Code
System.out.print("Enter in the course name: ");
String courseName = s.nextLine();
while(courseName.equals("")){
System.out.println("You must enter a course name.");
System.out.print("Enter in the course name: ");
courseName = s.nextLine();
}


But the requirements for my project says I must use exception handling.

my program works the way it is and makes the user enter in something and not just press enter, i am just confused how to throw this exception if it isn't usually caught by the computer anyways.

I know how to throw NumberFormatExceptions
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 18 2013 10:33pm
Quote
i am just confused how to throw this exception if it isn't usually caught by the computer anyways.


throw new WhateverMyExceptionIs("possibly some arg");

This post was edited by carteblanche on Nov 18 2013 10:33pm
Member
Posts: 29,197
Joined: Feb 5 2007
Gold: 4,000.18
Nov 18 2013 11:02pm
Quote (carteblanche @ Nov 19 2013 12:33am)
throw new WhateverMyExceptionIs("possibly some arg");


And make sure your code is in a try block.
Member
Posts: 2,736
Joined: Nov 28 2009
Gold: 34.00
Nov 19 2013 02:35am
Quote (IsraeliSoldier @ 19 Nov 2013 07:02)
And make sure your code is in a try block.


Huh? No, a throw doesn't need to be inside a try block. See http://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html
Member
Posts: 29,197
Joined: Feb 5 2007
Gold: 4,000.18
Nov 19 2013 02:03pm
Quote (Eagl3s1ght @ Nov 19 2013 04:35am)
Huh? No, a throw doesn't need to be inside a try block. See http://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html


Good practice.
Member
Posts: 11,637
Joined: Feb 2 2004
Gold: 434.84
Nov 19 2013 02:54pm
Quote (IsraeliSoldier @ Nov 19 2013 03:03pm)
Good practice.


If you are throwing an explicit exception why would you enclose that in a try block? That doesn't make sense at all, example:

Code

public void printString(String s) {
if (s == null) {
throw new NullPointerException("Parameter 1 should not be null!");
}

System.err.println(s);
}

Member
Posts: 20,928
Joined: Mar 18 2009
Gold: 435,910.13
Nov 19 2013 05:26pm
nm

This post was edited by bakalolo on Nov 19 2013 05:49pm
Member
Posts: 20,928
Joined: Mar 18 2009
Gold: 435,910.13
Nov 19 2013 05:42pm
Quote (GetSpanked @ Nov 18 2013 06:26pm)
right now I have this:

Code
System.out.print("Enter in the course name: ");
  String courseName = s.nextLine();
  while(courseName.equals("")){
    System.out.println("You must enter a course name.");
    System.out.print("Enter in the course name: ");
    courseName = s.nextLine();
  }


But the requirements for my project says I must use exception handling.

my program works the way it is and makes the user enter in something and not just press enter, i am just confused how to throw this exception if it isn't usually caught by the computer anyways.

I know how to throw NumberFormatExceptions



you do something like this

Code
void function(){
if(courseName.length==0)
throw Exception..
}

//in main
try{
function();
}catch(Exception e){

}


you will catch the exception in watever class that calls function if that makes any sense...

This post was edited by bakalolo on Nov 19 2013 06:01pm
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll