tags:

views:

46

answers:

1

Here's the first exception coding I've ever done and guess what, it's generating an error. Sad.

public class Exc {
int x = 2;
public void throwE(int p) throws Excp { 
    if(x==p) {
        throw new Excp();
    }
  }
}

I don't think I need to post the handler code as even this class isn't getting through compiler.

I'm getting the error cannot find symbol at Excp. I'm doing exactly according book. Is there something I'm missing?

+5  A: 

You are probably missing an Excp class. Try replacing Excp with Exception, for starters.

Tedil
um.. that worked. But please, can you explain what I was doing wrong. I mean, what's wrong with the name of the exception?
MoonStruckHorrors
An exception needs to exist. Did you want the class itself to be the exception, then you need to spell it the same.
Thorbjørn Ravn Andersen
`Excp` is not a class from the Java Standard Library. `Exception` is. If you want to throw your own exception, such as `Excp` or `MyException` or `WhateverNameYouWant` you have to create such class and it has to extend `Exception` class. You can do so creating a new class like this: `public class MyException extends Exception { //here the implementation}`
pakore
Your class is called `Exc` but you are throwing an `Excp`, so the compiler can't find anything actually called `Excp` and is complaining.
klez
All of you, Thanks a lot. Got It!
MoonStruckHorrors