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?

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

5 Comments

um.. that worked. But please, can you explain what I was doing wrong. I mean, what's wrong with the name of the exception?
An exception needs to exist. Did you want the class itself to be the exception, then you need to spell it the same.
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}
Your class is called Exc but you are throwing an Excp, so the compiler can't find anything actually called Excp and is complaining.
All of you, Thanks a lot. Got It!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.